【问题标题】:Binding to a Property of UserControl绑定到 UserControl 的属性
【发布时间】:2016-06-20 13:45:48
【问题描述】:

我编写了一个包含很多东西的小部件,它包含一个从 UC 类内部更新的属性 (DependencyProperty)(基于输入到文本框中的输入以及来自按钮单击的输入等)。它的定义是:

public partial class UserControlClassName : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    } 

    public static readonly DependencyProperty ValueProperty = System.Windows.DependencyProperty.Register("Value", typeof(string), typeof(UserControlClassName), new PropertyMetadata(string.Empty, OnValueChanged));

    public string Value
    {
        get { return (string)GetValue(ValueProperty); }
        set 
        { 
            SetValue(ValueProperty, value);
            NotifyPropertyChanged("Value");
        }
    }

    private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue != null && e.NewValue != e.OldValue)
        {
            Console.Out.WriteLine("new value: " + e.NewValue);
        }
    }

   ...
   ...
   lots of other code that updates the Value property..
   ...
   ...

}

我正在某些窗口的 XAML 中实例化 UC,如下所示:

<GeneralControls:UserControlClassName
            x:Name="someRandomName"
            Value="{Binding MyViewModel.MyBoundedValueField, StringFormat=N2, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DebugBinding}}"
            VerticalAlignment="Bottom" />

为了完成图片,这是我用来“获取”值的视图模型的属性:

public string MyBoundedValueField
    {
        get { return myBoundedValueField; }
        set
        {
            if (myBoundedValueField!= value)
            {
                myBoundedValueField = value;
                OnPropertyChanged("MyBoundedValueField");
            }
        }
    }

我的问题是 - 用户控件 do 内的 Value 属性得到更新,但我在 xaml (myBoundedValueField ) 中绑定到的外部属性没有得到更新......绑定到这个依赖属性不起作用-所以我附加了一个转换器来调试它并且转换器没有被调用所以它肯定是一个错误的绑定设置..

(Tnx 给任何帮助的人!)

【问题讨论】:

  • MyViewModel.MyBoundedValueField 这样的绑定路径不设置Source 或RelativeSource 将需要在UserControl 或其父控件之一的DataContext 中具有公共MyViewModel 属性的对象。除此之外,依赖属性不需要实现 INotifyPropertyChanged。所以你可以从你的代码中删除很多多余的东西。
  • 您当然应该将MyViewModel 的实例传递给UserControl 的DataContext 并将绑定路径更改为{Binding MyBoundedValueField, ...}
  • 在常见场景中(例如,在 ItemsControl 的 ItemTemplate 中),UserControl 将通过继承其父项的 DataContext 属性的值来继承其数据对象(即其绑定的源对象)。如果您有一个主视图模型,您通常会将 MainWindow 的 DataContext 分配给该视图模型的一个实例,例如 &lt;Window.DataContext&gt;&lt;local:MyViewModel&gt;&lt;/Window.DataContext&gt;
  • 那是具有MyBoundedValueField 属性的(视图模型)类的一个实例?绑定路径应该只是MyBoundedValueField 而不是MyViewModel.MyBoundedValueField
  • 好吧,我猜不出你到底有什么。 MyViewModel 也可能是您的“主”视图模型的公共属性。你没有在你的问题中表明这一点。

标签: c# wpf data-binding user-controls


【解决方案1】:

最终的解决方案是添加 Mode=TwoWay

<GeneralControls:UserControlClassName
    x:Name="someRandomName"
    Mode=TwoWay
    Value="{Binding MyViewModel.MyBoundedValueField, StringFormat=N2, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DebugBinding}}"
    VerticalAlignment="Bottom" />

我不知道为什么没有它绑定就无法工作 - 我很想听听解释,如果有任何 1 可以对这个主题有所启发..

特别感谢@Clemens!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多