2017-03-14 74 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

然後片段就會

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是事件的PropertyChanged 當設置isLocalSearchEnabled的值(無論舊值和新值)被觸發,OnPropertyChanged叫什麼在這裏發生與公共屬性的名稱

+0

謝謝。但正如在帖子中提到的,我實際上使用了Fody(github.com/Fody/PropertyChanged)。其中自動實現, –

0

看來你還沒有實現INotifyPropertyChanged接口,請參閱詳細的例子INotifyPropertyChanged

+1

謝謝。但正如在帖子中提到的,我實際上使用了Fody(https://github.com/Fody/PropertyChanged)。自動實現的。 –

0

您是否設置了assetsStackPanel DataContext綁定需要的源,您應該設置DataContext ont只設置源。

如果你在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的財產知道代碼是否正確。

而且你可以在XAML中使用BindingOperations.SetBinding

嘗試使用ReSharper的,寫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

相關問題