2017-03-14 3 views
1

コードの背後に動的コントロールを作成し、そのコードのプロパティの可視性プロパティをコードの背後に設定して設定しています。しかし、プロパティ値が変更された場合、コントロールの可視性は更新されません。コード経由で設定したときに双方向バインディングが機能しない

バインディング:

 Binding assetsVisibilityBinding = new Binding(); 
     assetsVisibilityBinding.Source = this; 
     assetsVisibilityBinding.Path = new PropertyPath("IsLocalSearchEnabled"); 
     assetsVisibilityBinding.Mode = BindingMode.TwoWay; 
     assetsVisibilityBinding.Converter = Resources["BooleanToVisibilityConverter"] as IValueConverter; 
     assetsStackPanel.SetBinding(StackPanel.VisibilityProperty, assetsVisibilityBinding); 

(fodyを使用する)プロパティ:

public bool IsLocalSearchEnabled { get; set; } 
+0

まず最初はjustDecompileにコンパイルされたバイナリアップを開き、確認fodyを作るです実際にあなたのアセンブリを書き換えています。また、実行時にバインドを調べるために、Snoopのようなツールを使用します(uwpsで動作するかどうかはわかりません)。 – Will

答えて

0

多分プロパティが含まれているクラスは

INotifyPropertyChanged 

はのは、あなたのクラス名を想定してみましょうインタフェースを実装する必要がありますbe A

は、あなたがINotifyPropertyChangedのはがisLocalSearchEnabledの値が(関係なく、古い値と新しい値の)設定されているとき がトリガされるとOnPropertyChangedをが呼び出されて、PropertyChangedイベントで実装したときに、ここで何が起こる

class A : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string propertyName) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public bool isLocalSearchEnabled = false; 
    public bool IsLocalSearchEnabled 
    { 
     get { return isLocalSearchEnabled ;} 
     set { isLocalSearchEnabled = value; this.OnPropertyChanged("IsLocalSearchEnabled"); 
    } 
} 

なりスニペットパブリックプロパティの名前であなたがINotifyPropertyChangedのインターフェイスを実装していないようだ

+0

ありがとうございます。しかし、私が実際にFody(github.com/Fody/PropertyChanged)を使用している記事の中で述べたように。これは自動的に実装します –

0

は、詳細な例を参照してください。INotifyPropertyChanged

+1

ありがとうございます。しかし、私は実際にFody(https://github.com/Fody/PropertyChanged)を使用している投稿に記載されているとおり。それが自動的に実装されます。 –

0

assetsStackPanel DataContextを設定しましたか?バインディングにはソースが必要です。ソースを設定するDataContextのみを設定する必要があります。

このプロパティをxaml.csに設定すると、公開する必要があります。

Binding assetsVisibilityBinding = new Binding(); 
     assetsVisibilityBinding.Source = this; 
     assetsVisibilityBinding.Path = new PropertyPath("IsLocalSearchEnabled"); 
     assetsVisibilityBinding.Mode = BindingMode.TwoWay; 
     assetsVisibilityBinding.Converter = Resources["BooleanToVisibilityConverter"] as IValueConverter; 
     assetsStackPanel.DataContex=this; 
     assetsStackPanel.SetBinding(StackPanel.VisibilityProperty, assetsVisibilityBinding); 

私はフレームワークを読んでいないために、私はあなたのコードが正しいかどうかを知るためにINotifyPropertyChangedのでプロパティを使用してみてくださいすることができると思います。

そして、あなたはBindingOperations.SetBinding

を使用することができますが、XAMLでReSharperのを使用して、それはそれはFrameworkが動作することができます意味で動作することができます.IF Visibility="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=IsLocalSearchEnabled}",mode=TwoWayを書く試してみてください。

0

ありがとうございます。しかし、私が実際に Fody(github.com/Fody/PropertyChanged)を使用している記事で述べたように。これは自動的に

こと 実装し、私は成功しを実施していなかったプロパティは、通知を変更Fody PropertyChangedを使用することにより、遵守クラスを確認しています。

[ImplementPropertyChanged] 
public sealed partial class MainPage : Page 
{ 
    public bool IsLocalSearchEnabled { get; set; } 

    public MainPage() 
    { 
     this.InitializeComponent(); 
     SetBinding(); 
     this.DataContext = this; 
    } 

    public void SetBinding() 
    { 
     Binding assetsVisibilityBinding = new Binding(); 
     assetsVisibilityBinding.Source = this; 
     assetsVisibilityBinding.Path = new PropertyPath("IsLocalSearchEnabled"); 
     assetsVisibilityBinding.Mode = BindingMode.TwoWay; 
     assetsVisibilityBinding.Converter = Resources["BooleanToVisibilityConverter"] as IValueConverter; 
     assetsStackPanel.SetBinding(StackPanel.VisibilityProperty, assetsVisibilityBinding); 
    } 
} 

問題をFodyに報告することをお勧めします。

標準的な方法を次のように

public sealed partial class MainPage : Page, INotifyPropertyChanged 
{ 
    bool isLocalSearchEnabled; 
    public bool IsLocalSearchEnabled 
    { 
     get { return isLocalSearchEnabled; } 
     set 
     { 
      if (value != isLocalSearchEnabled) 
      { 
       isLocalSearchEnabled = value; 
       OnPropertyChanged("IsLocalSearchEnabled"); 
      } 
     } 
    } 

    public MainPage() 
    { 
     this.InitializeComponent(); 
     SetBinding(); 
     this.DataContext = this; 
    } 

    public void SetBinding() 
    { 
     Binding assetsVisibilityBinding = new Binding(); 
     assetsVisibilityBinding.Source = this; 
     assetsVisibilityBinding.Path = new PropertyPath("IsLocalSearchEnabled"); 
     assetsVisibilityBinding.Mode = BindingMode.TwoWay; 
     assetsVisibilityBinding.Converter = Resources["BooleanToVisibilityConverter"] as IValueConverter; 
     assetsStackPanel.SetBinding(StackPanel.VisibilityProperty, assetsVisibilityBinding); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged(string propertyName) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

それとも、簡単にラッパークラスを使用することができます私は思いBindableBase

関連する問題