我疯狂地使用自定义的依赖属性。我已经在这里检查了大量线程,但还没有找到任何解决方案。我想要做的是如果源提供特定的值(给定示例为null),则替换该属性的值。无论我尝试什么,源中的属性值都保持为空,并且从不更新。依赖属性 - 更新源
这里是我的自定义控制:
public class TextBoxEx : TextBox
{
public TextBoxEx()
{
TrueValue = 0;
this.TextChanged += (s, e) =>
{
TrueValue = Text.Length;
SetCurrentValue(MyPropertyProperty, TrueValue);
var x = BindingOperations.GetBindingExpression(this, MyPropertyProperty);
if (x != null)
{
x.UpdateSource();
}
};
}
public int? TrueValue { get; set; }
public int? MyProperty
{
get { return (int?)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int?), typeof(TextBoxEx), new PropertyMetadata(null, PropertyChangedCallback));
private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue == null)
{
d.SetCurrentValue(MyPropertyProperty, (d as TextBoxEx).TrueValue);
}
}
}
这里是我绑定的DataContext:
public class VM : INotifyPropertyChanged
{
private int? _Bar = null;
public int? Bar
{
get { return _Bar; }
set
{
_Bar = value;
OnPropertyChanged("Bar");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我结合这个样子的:
<local:TextBoxEx MyProperty="{Binding Bar, UpdateSourceTrigger=PropertyChanged}"/>
记住:我需要一个双向绑定,所以OneWayToSource不适合我。
任何想法,我不在这里?
不错!如果可以的话,会超过+1!有一个自定义用户控件,其属性绑定到子控件属性中,但不会将值传递给演示者。正在看各种各样的东西。讨厌wpf有多少魔法标志,但是它在工作的时候很棒! –
很高兴帮助,是的,有很多知道,但它确实是一个很好的框架,如果它的工作:) –
谢谢,这个答案救了我这么多时间。 – Orwel