2016-10-18 5 views
1

私はXAMLにカスタムTriggerActionを宣言しており、それにコマンドをバインドしています。依存プロパティGetValueがnullを返す

<DataTemplate x:Key="data"> 
     <StackPanel Orientation="Vertical"> 
      <TextBlock Text="{Binding Name}"/> 
      <TextBox Width="150" Name="styleTb"> 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="TextChanged"> 
         <behaviors:TextChangedTrigger TextChangedCommand="{Binding TextChangedCommand}"/> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
      </TextBox> 
     </StackPanel> 
    </DataTemplate> 

しかし、私はGetValueメソッドを介して値を取得しようとすると、それはnullを返します。

このクラスは、なぜそれがnullを返すん

public class TextChangedTrigger : TriggerAction<TextBox> 
{ 
    public static readonly DependencyProperty TextChangedCommandProperty = DependencyProperty.Register("TextChangedCommand", typeof(ICommand), 
                             typeof(TextChangedTrigger)); 

    /// <summary> 
    /// Gets the Command which will be executed. 
    /// </summary> 
    public ICommand TextChangedCommand 
    { 
     get { return (ICommand)GetValue(TextChangedCommandProperty); } 
     set { SetValue(TextChangedCommandProperty, value); } 
    } 

    /// <summary> 
    /// Invokes the TextChangedCommand 
    /// </summary> 
    /// <param name="parameter"></param> 
    protected override void Invoke(object parameter) 
    { 
     object test = GetValue(TextChangedCommandProperty); // Returns null 
    } 

どのように見えるかですか?

+0

「object test = TextChangedCommand;」を使用するのはどうでしょうか? –

+0

@MikeEasonそれは動作しません – Coding4Fun

+0

私の答えを参照してください。私はそれを編集した – Coding4Fun

答えて

0

TextChangedCommandプロパティの値を実際に設定することはありません。

TextChangedTriggerクラスのコンストラクタに値を設定するか、既定値を設定できるオーバーヘッドDependencyProperty.Registerを使用します。

public static readonly DependencyProperty TextChangedCommandProperty = DependencyProperty.Register("TextChangedCommand", 
    typeof(ICommand), 
    typeof(TextChangedTrigger), 
    new UIPropertyMetadata(new WhateverYourCommandIs()); 
関連する問題