通过代码设置

时间:2017-03-14 07:12:41

标签: c# xaml mvvm uwp winrt-xaml

我在后面的代码中创建动态控件,并将其可见性属性绑定到后面的代码中的属性。但是当属性值发生变化时,它不会更新控件的可见性。

结合:

        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; }

4 个答案:

答案 0 :(得分:0)

也许你的包含属性的类需要实现接口

INotifyPropertyChanged

我们假设您的班级名称为 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

答案 1 :(得分:0)

您似乎还没有实现INotifyPropertyChanged界面,请参阅详细示例INotifyPropertyChanged

答案 2 :(得分: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);

因为我没有读过frameWork,我想你可以尝试使用INotifyPropertyChanged的属性来知道代码是否正确。

您可以使用BindingOperations.SetBinding

尝试在xaml中使用resharper并编写Visibility="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=IsLocalSearchEnabled}",mode=TwoWay。如果它可以工作则意味着FrameWork可以工作。

答案 3 :(得分: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