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

时间:2016-11-28 22:29:15

标签: c# wpf data-binding enums dependency-properties

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

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));
    }
}

我的应用程序Ressources(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>

错误列表中未显示任何错误。但是当我构建解决方案时,一个消息框告诉我:“属性值无效”

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

1 个答案:

答案 0 :(得分:0)

经过一番摆弄后,我记得当我复制/拼写你的代码时,我没有设置数据上下文。在将一些datatrigger绑定和值修复为您已经使用的值之后,这就成功了。这是我改变的,它可以在我的电脑上运行:

的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;
    }
    ...
}