如何从XAML访问全局命名空间中的枚举



我需要使用一个库,该库在任何命名空间之外声明了各种类型。我正试图使用一个以这种方式声明的枚举作为DataTrigger的一部分,但我不知道如何引用它

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <l:ViewModel />
    </Window.DataContext>
    <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="LightBlue" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding EnumVal}" Value="{x:Static l:MyEnum.Val1}">
                    <Setter Property="Background" Value="LightGreen" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Content="Test">
        </Button>
    </Grid>
</Window>

ViewModel为(注意,MyEnum是在所有命名空间之外声明的):

public enum MyEnum
{
    Val1,
    Val2
}
namespace WpfApplication2
{
    public class ViewModel
    {
        public ViewModel()
        {
            EnumVal = MyEnum.Val1;
        }
        public MyEnum EnumVal { get; set; }
    }
}

像这样:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:global="clr-namespace:"
    mc:Ignorable="d"
    Title="MainWindow" Height="500" Width="500">
    <Button Content="{x:Static global:MyEnum.Val1}" />
</Window>

最新更新