自分の依存関係プロパティに基づいてボタンのスタイルを変更しようとしています。私はなぜこれが動作していないのか理解していないようです。列挙型とバインドする方法と関係があります。私は新しいWPFとive永遠に検索しています。助けてください!関連コード 最初に私のボタンクラス:enum依存関係プロパティを持つボタンのスタイルを変更します。適切にバインドする方法
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):
<local:DLSButton ammoTypeEnum="EMPTY" x:Name="buttonDLS1" Style="{StaticResource DLSAmmo}">
</local:DLSButton>
エラーがエラーリストに示されている:私は私のボタンを挿入
<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 。しかし、ソリューションをビルドすると、メッセージボックスに「プロパティ値が有効ではありません」というメッセージが表示されます。
「DataTriggers」でわかるように、私はバインディングでさまざまなことを試しました。まだエラーはありません。まだスタイルが変わっていません...
あなたの結合パスは、実際のプロパティにあります。あなたの場合、* ammoTypeEnumとなるかもしれませんが、それを伝えるのは難しいでしょう。あなたはあなたが得ている例外についてより具体的になることができますか? – BradleyDotNET
なぜ静的メソッド内でプロパティを初期化していますか?宣言の残りの部分を上にして初期化しないのはなぜですか? – Meloviz
また、ボックスは実際には何と言いませんか?それは行番号を与えますか?内部の例外は "プロパティ値が有効ではありません"ですか? – Meloviz