wpf 如何以及在何处为简单 XAML 窗口中的转换器创建静态资源密钥?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34351693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How and Where to Create StaticResource Key for a Converter within the Simple XAML Window?
提问by B.Balamanigandan
I'm having a simple WPF XAML Window, I need to Create a StaticResource Key with in the following XAML.
我有一个简单的 WPF XAML 窗口,我需要在以下 XAML 中创建一个静态资源密钥。
The XAML Source Code is
XAML 源代码是
<Window x:Class="WpfApplication1.Trigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:super="clr-namespace:WpfApplication1"
Title="Trigger" Height="300" Width="300">
<Grid>
<Border x:Name="m_Border" Width="100" Height="30" HorizontalAlignment="Center" VerticalAlignment="Top" Background="#FFF2FFC6" Margin="0,20,0,0">
<Button x:Name="btn" Content="iApp" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Visibility="{Binding IsMouseOver,ElementName=m_Border, Converter={StaticResource BooleanToVisibilityConverterKey}, ConverterParameter=Normal}"/>
</Border>
</Grid>
</Window>
My Converter C# Source Code:
我的转换器 C# 源代码:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication1
{
public enum BooleanToVisibilityConverterType
{
Normal = 1,
Reverse = 2
}
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var targertValue = false;
if (value == null)
{
throw new Exception("BooleanToVisibilityConverter - Convert Error");
}
else if (!Boolean.TryParse(value.ToString(), out targertValue))
{
throw new Exception("BooleanToVisibilityConverter - Convert Error");
}
else
{
var parameterValue = BooleanToVisibilityConverterType.Normal;
if (parameter != null)
{
Enum.TryParse<BooleanToVisibilityConverterType>(parameter.ToString(), out parameterValue);
}
if (parameterValue == BooleanToVisibilityConverterType.Reverse)
{
return targertValue ? Visibility.Collapsed : Visibility.Visible;
}
else
{
return targertValue ? Visibility.Visible : Visibility.Collapsed;
}
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var targetValue = Visibility.Collapsed;
if (value == null)
{
throw new Exception("BooleanToVisibilityConverter - ConvertBack Error");
}
else if (!Enum.TryParse<Visibility>(value.ToString(), out targetValue))
{
throw new Exception("BooleanToVisibilityConverter - ConvertBack Error");
}
else
{
var parameterValue = BooleanToVisibilityConverterType.Normal;
if (parameter != null)
{
Enum.TryParse<BooleanToVisibilityConverterType>(parameter.ToString(), out parameterValue);
}
if (parameterValue == BooleanToVisibilityConverterType.Reverse)
{
return targetValue == Visibility.Visible ? false : true;
}
else
{
return targetValue == Visibility.Visible ? true : false;
}
}
}
}
}
I need a Converter Key with Name BooleanToVisibilityConverterKeyfor the Converter BooleanToVisibilityConverter
我需要一个名称为BooleanToVisibilityConverterKey的 Converter Key 用于 Converter BooleanToVisibilityConverter
采纳答案by Mike Eason
You can define your Converterin the Window.Resourceselement.
您可以Converter在Window.Resources元素中定义您的。
<Window ...
>
<Window.Resources>
<super:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverterKey"/>
</Window.Resources>
...
It could be a better idea to make this converter global. This will save you from having to define the converter in every new Window. It also means that your converter is only instantiated once, therefore improving performance slightly.
将此转换器设为global可能是一个更好的主意。这将使您不必在每个新的Window. 这也意味着您的转换器仅实例化一次,因此性能略有提高。
To achieve this, define the converter in App.xamlinstead.
要实现这一点,请App.xaml改为定义转换器。
<Application ...
xmlns:super="clr-namespace:WpfApplication1">
<Application.Resources>
<super:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverterKey"/>
</Application.Resources>
</Application>
回答by O. R. Mapper
You usually put this into the Resourcespropertyof the surrounding object, in this case, your Window:
您通常将其放入周围对象的Resources属性中,在这种情况下,您的Window:
<Window x:Class="WpfApplication1.Trigger"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:super="clr-namespace:WpfApplication1"
Title="Trigger" Height="300" Width="300">
<Window.Resources>
<super:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverterKey"/>
</Window.Resources>
<Grid>
...
Some things to note:
一些注意事项:
- Do not forget to use the appropriate namespace prefix.
- Strictly speaking, what is happening here is not merely that you are "defining a key"; you are putting an instanceof your converter class into the local resource dictionary and assigning that instance to a key.
- By convention, you would not normally name resource keys ...Keyexplicitly. So-to-speak, in the rest of your XAML document, the resource key isthe object stored in the resource. You would name propertiesthat dynamically return certain keys ...Key(such as various of the properties in the
SystemColorsclass).
- 不要忘记使用适当的命名空间前缀。
- 严格来说,这里发生的事情不仅仅是你在“定义一个键”;您正在将转换器类的一个实例放入本地资源字典并将该实例分配给一个键。
- 按照惯例,您通常不会显式命名资源键...Key。可以这么说,在 XAML 文档的其余部分,资源键是存储在资源中的对象。你会说出性能动态返回某些键...按键(如各种的属性的
SystemColors类)。

