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

问题描述:

我想基于它自己的依赖项属性来改变我的按钮的样式。我似乎无法弄清楚为什么这不起作用。它必须处理枚举以及如何绑定它。我是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其中i插入我的按钮:

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

没有错误显示在错误列表。但是当我构建解决方案时,消息框告诉我:“属性值无效”

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

+0

你绑定路径是实际财产;在你的情况下,它*可能是'ammoTypeEnum',但很难说。你能更具体地了解你所得到的例外吗? – BradleyDotNET

+0

你为什么在一个静态方法中初始化你的属性?为什么不把它的声明的其余部分进行初始化? – Meloviz

+0

另外,盒子实际上是什么字样?它是否给出了行号?内部异常“属性值无效”? – Meloviz

经过一番捣鼓之后,我记得当我复制/ pasta'd你的代码时,我没有设置数据上下文。在将一些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; 
    } 
    ... 
} 
+0

可以请您发布完整的代码吗?我的依赖属性不能放在公共DLSButton()类中,因为它是一个静态属性 –

+0

没关系。我得到它的工作非常感谢你。该解决方案如您所描述的那样简单。设置'datacontext = this;'并将依赖项属性向上移动。 –

+0

很高兴我可以帮助:)我实际上使用您的确切代码,除了我在这里发布的编辑。唯一的主要区别是在DLSButton的构造函数中添加。我遇到了很多似乎只是一件事的问题,但最终却忘了添加数据上下文。 – Meloviz