[英]change style of button with enum dependency property. How to properly bind
我試圖根據自己的依賴項屬性更改按鈕的樣式。 我似乎無法弄清楚為什么這不起作用。 它與枚舉以及如何綁定它有關。 我是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));
}
}
我的應用程序資源(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”中看到的那樣,我嘗試了不同的綁定操作。 仍然沒有錯誤。 仍然沒有樣式改變...
經過一番擺弄之后,我記得復制/粘貼您的代碼時沒有設置數據上下文。 在將某些數據觸發綁定和值固定為您已經在使用的值之后,就可以達到目的。 這是我所做的更改,並且可以在我的計算機上使用:
應用程式
// 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;
}
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.