如何以及在何处为简单 XAML Window 中的转换器创建 StaticResource 密钥?

How and Where to Create StaticResource Key for a Converter within the Simple XAML Window?

我有一个简单的 WPF XAML Window,我需要使用以下 XAML.

创建一个 StaticResource 键

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>

我的转换器 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 BooleanToVisibilityConverterKey for the Converter BooleanToVisibilityConverter

您可以在 Window.Resources 元素中定义 Converter

<Window ...
        >
    <Window.Resources>
        <super:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverterKey"/>
    </Window.Resources>
    ...

让这个转换器全球可能是个更好的主意。这将使您不必在每个新 Window 中定义转换器。这也意味着您的转换器仅被实例化 一次,因此性能略有提高。

要实现这一点,请改为在 App.xaml 中定义转换器。

<Application ...
             xmlns:super="clr-namespace:WpfApplication1">
    <Application.Resources>
        <super:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverterKey"/>
    </Application.Resources>
</Application>

你通常把这个放到Resources property of the surrounding object, in this case, your 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>
...

一些注意事项:

  • 不要忘记使用适当的命名空间前缀。
  • 严格来说,这里发生的不仅仅是你是"defining a key";您正在将转换器 class 的 实例 放入本地资源字典并将该实例分配给键。
  • 按照惯例,您通常不会明确命名资源键 ...Key。可以这么说,在您的 XAML 文档的其余部分中,资源键 存储在资源中的对象。您可以将 properties 命名为动态地 return 某些键 ...Key(例如 [=15= 中的各种属性) ]).