如何对usercontrol属性进行双向绑定(Public String)
本文关键字:绑定 Public String usercontrol 属性 | 更新日期: 2023-09-27 18:18:29
查找所有关于如何在usercontrol中绑定控件的结果。但我现在的问题很简单,就是把值绑定到a中公共字符串MyBindingString {get;设置;}
我试过了,但是当进入那个页面时出现了一个错误设置属性"xxx.xxx.xxx .xx"。MyBindingString'抛出异常
谢谢你的帮助。
现在,我发现当我在代码中绑定的时候就像"Text={Binding MyCustomText, Mode= two - way}"
实际传递给MyBindingString的是class: System.Windows.Data.Binding
任何想法?
如果问题的标题定义明确。你要找的是"Mode"
for two way, Mode= two way,例子:
"Text={Binding MyCustomText, Mode=TwoWay}"
如果你想在UserControl中创建可绑定的属性,可以这样做:
#region MyCustomText
public string MyCustomText
{
get { return GetValue(MyCustomTextProperty) as string; }
set { SetValue(MyCustomTextProperty, value); }
}
public static readonly DependencyProperty MyCustomTextProperty = DependencyProperty.Register("MyCustomText",
typeof(string), typeof(MyUserControl), new System.Windows.PropertyMetadata(null, new PropertyChangedCallback(OnMyCustomTextChanged)));
public static void OnMyCustomTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var control = sender as MyUserControl;
var value = e.NewValue as string;
}
#endregion MyCustomText