更改具有枚举依赖性的按钮样式 属性。如何正确绑定

change style of button with enum dependency property. How to properly bind

我正在尝试根据按钮自身的依赖性更改按钮的样式 属性。我似乎无法弄清楚为什么这不起作用。它与枚举以及如何绑定它有关。我是 WPF 的新手,我一直在寻找。请帮忙! 相关代码。首先是我的按钮 class:

public class AmmoType
{
    public enum ammoType { RFS, RFD, RFC, EMPTY }
}

public class DLSButton : Button
{
    public static readonly DependencyProperty AmmoTypeProperty;
    public AmmoType.ammoType ammoTypeEnum
    {
        get
        {
            return (AmmoType.ammoType)GetValue(DLSButton.AmmoTypeProperty);
        }
        set
        {
            SetValue(DLSButton.AmmoTypeProperty, value);
        }
    }
    static DLSButton()
    {
        DLSButton.AmmoTypeProperty = DependencyProperty.Register("ammoTypeEnum", typeof(AmmoType.ammoType), typeof(DLSButton), new FrameworkPropertyMetadata(AmmoType.ammoType.EMPTY));
    }
}

我的应用资源(App.xml):

<Application.Resources>        
    <Style TargetType="{x:Type local:DLSButton}" x:Key="DLSAmmo">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Background" Value="LightGray"/>
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="Margin" Value="1,1,1,1" />
        <Setter Property="IsEnabled" Value="False"/>
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Viewbox StretchDirection="Both" >
                        <TextBlock FontWeight="Bold" TextWrapping="Wrap">DLS</TextBlock>
                    </Viewbox>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=AmmoType+ammoType}" Value="{x:Static my:AmmoType+ammoType.EMPTY}">

                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Background" Value="Red"/>
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="Margin" Value="1,1,1,1" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Viewbox StretchDirection="Both" >
                                <TextBlock FontWeight="Bold" TextWrapping="Wrap">N/A</TextBlock>
                            </Viewbox>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ammoTypeEnum}" Value="RFD">

                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Background" Value="Green"/>
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="Margin" Value="1,1,1,1" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Viewbox StretchDirection="Both" >
                                <TextBlock FontWeight="Bold" TextWrapping="Wrap">RFD</TextBlock>
                            </Viewbox>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ammoTypeEnum}" Value="{x:Static local:AmmoType+ammoType.RFS}">

                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Background" Value="Green"/>
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="Margin" Value="1,1,1,1" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Viewbox StretchDirection="Both" >
                                <TextBlock FontWeight="Bold" TextWrapping="Wrap">RFS</TextBlock>
                            </Viewbox>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ammoTypeEnum}" Value="{x:Static local:AmmoType+ammoType.RFC}">

                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Background" Value="Green"/>
                <Setter Property="VerticalAlignment" Value="Stretch" />
                <Setter Property="Margin" Value="1,1,1,1" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Viewbox StretchDirection="Both" >
                                <TextBlock FontWeight="Bold" TextWrapping="Wrap">RFC</TextBlock>
                            </Viewbox>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>                
    </Style>
</Application.Resources>

我的 XAML 我插入按钮的地方:

<local:DLSButton ammoTypeEnum="EMPTY" x:Name="buttonDLS1" Style="{StaticResource DLSAmmo}">                            
                    </local:DLSButton>

错误列表中未显示任何错误。但是当我构建解决方案时,一个消息框告诉我:"the property value is not valid"

正如您在不同 'DataTriggers' 中看到的那样,我尝试了不同的绑定方法。仍然没有错误。仍然没有改变风格...

经过一番调整后,我想起来我在 copy/pasta 编写您的代码时没有设置数据上下文。在将一些数据触发器绑定和值修复为您已经使用的值之后,这就成功了。这是我所做的更改,它在我的计算机上有效:

App.xaml

 // so we can see the bg change, but the text changed without it
<Setter Property="IsEnabled" Value="True"/>
...
// for each of the datatriggers
<DataTrigger Binding="{Binding Path=ammoTypeEnum}" 
             Value="{x:Static local:AmmoType+ammoType.XXX}">

DLSButton.cs

// this should be obvious
public partial class DLSButton : Button
{
    public DLSButton()
    {
        InitializeComponent();
        DataContext = this;
    }
    ...
}