2016-11-28 1 views
0

自分の依存関係プロパティに基づいてボタンのスタイルを変更しようとしています。私はなぜこれが動作していないのか理解していないようです。列挙型とバインドする方法と関係があります。私は新しい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」でわかるように、私はバインディングでさまざまなことを試しました。まだエラーはありません。まだスタイルが変わっていません...

+0

あなたの結合パスは、実際のプロパティにあります。あなたの場合、* ammoTypeEnumとなるかもしれませんが、それを伝えるのは難しいでしょう。あなたはあなたが得ている例外についてより具体的になることができますか? – BradleyDotNET

+0

なぜ静的メソッド内でプロパティを初期化していますか?宣言の残りの部分を上にして初期化しないのはなぜですか? – Meloviz

+0

また、ボックスは実際には何と言いませんか?それは行番号を与えますか?内部の例外は "プロパティ値が有効ではありません"ですか? – Meloviz

答えて

0

いくつかの手抜きをした後、私はあなたのコードをコピー/パスタしたときにデータコンテキストを設定していないことを思い出しました。これは、データトリガーのバインディングと値のいくつかをすでに作業していた値に修正した後にトリックを行いました。ここで私は変更するものだし、それは私のコンピュータ上で動作します:

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

完全なコードを投稿してください。私の依存関係プロパティは、静的プロパティ –

+0

であるため、パブリックDLSButton()クラスに配置することはできません。私はそれを働かせてくれてありがとう。解決策はあなたが説明したほど簡単でした。設定 'datacontext = this;'依存関係プロパティを上に移動します。 –

+0

私は助けてくれると嬉しかった:)私はここで投稿した編集を除いて実際にあなたの正確なコードを使用しました。唯一の大きな違いは、DLSButtonのコンストラクタでの追加です。私は多くの問題に遭遇しましたが、それは1つのことと思われますが、データ・コンテキストを追加するのを忘れてしまったことになります。 – Meloviz

関連する問題