[英]Binding to a Property of UserControl
I wrote a widget that contains lots of stuff, and it contains a property (DependencyProperty) that is being updated from inside the UC class (based on input being typed into a textbox and input from button clicks etc..) Its definition is : 我写了一个包含很多东西的小部件,它包含一个属性(DependencyProperty),该属性正在UC类内部进行更新(基于在文本框中键入的输入和来自按钮单击等的输入。)其定义是:
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..
...
...
}
I'm instantiating the UC in some window's XAML as follows: 我在某些窗口的XAML中实例化UC,如下所示:
<GeneralControls:UserControlClassName
x:Name="someRandomName"
Value="{Binding MyViewModel.MyBoundedValueField, StringFormat=N2, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DebugBinding}}"
VerticalAlignment="Bottom" />
And to complete the picture, this is the view model's property i'm using to "grab" the value : 为了完成图片,这是我用来“抓取”值的视图模型的属性:
public string MyBoundedValueField
{
get { return myBoundedValueField; }
set
{
if (myBoundedValueField!= value)
{
myBoundedValueField = value;
OnPropertyChanged("MyBoundedValueField");
}
}
}
My problem is- the Value property that inside the user control do get updated, but the outer property i'm binding to in the xaml (myBoundedValueField ) is not getting the updates.. something with the binding to this dependency property is not working- so I attached a converter to debug this and the converter does not get called so its definitely a wrong binding setup.. 我的问题是-用户控件内的Value属性确实得到了更新,但是我绑定到xaml(myBoundedValueField)中的外部属性没有获得更新..与此绑定属性绑定的某些东西不起作用-所以我附加了一个转换器来调试它,并且转换器没有被调用,所以它肯定是错误的绑定设置。
(Tnx to any1 who help!) (向任何帮助的人致谢!)
The solution eventually was to add Mode=TwoWay 解决方案最终是添加Mode = TwoWay
<GeneralControls:UserControlClassName
x:Name="someRandomName"
Mode=TwoWay
Value="{Binding MyViewModel.MyBoundedValueField, StringFormat=N2, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DebugBinding}}"
VerticalAlignment="Bottom" />
I don't know as to why the binding did not work without it - I would love to hear an explanation if any1 can shade some light on the subject.. 我不知道为什么没有它,装订无法工作-我很想听听一个解释,说明any1可以使这个主题有些阴影。
Special thanks to @Clemens!!! 特别感谢@Clemens !!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.