我的依赖属性仅限于一种方式 [英] My Dependency Property is only binding one way
问题描述
这是我的XAML(为了可读性,我剥离了其他控件): / p>
< UserControl x:Class =WindowsApp.FormBox
xmlns =http://schemas.microsoft。 com / winfx / 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:local =clr-namespace:WindowsApp >
< Grid DataContext ={Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = local:FormBox}}>
< TextBox x:Name =TextFormText ={Binding Path = Value,Mode = TwoWay}/>
< / Grid>
< / UserControl>
这里的c#,Visual Studio为我自动生成,所以也许有一些不正确的
public String Value
{
get {return(String)GetValue(ValueProperty); }
set
{
SetValue(ValueProperty,value);
}
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(Value,typeof(String),typeof(FormBox));
在代码的其他部分,我只需通过执行 Value ={Binding Whatever}
任何建议或帮助将不胜感激。谢谢。
您需要将Text绑定的UpdateSourceTrigger设置为PropertyChanged。 TextBox的默认值为LostFocus。如果这是一个窗口中唯一的控件,那么没有什么可以接收焦点,所以一个LostFocus事件永远不会触发,绑定永远不会更新源。
< TextBox x:Name =TextFormText ={Binding Path = Value,UpdateSourceTrigger = PropertyChanged,Mode = TwoWay}/>
I'm new to WPF, and I'm doing something which I feel is pretty straightforward. I made a UserControl that contains a textbox, and I want to bind the TextBox.Text property to a dependency property of my usercontrol, Value. The textbox correctly displays Value, and if you change the value of Value, the textbox updates accordingly. But, if I change the value in the Textbox, Value does not change to reflect it. Even if I manually set the binding Mode to two-way, it still only binds one way. I made Value a dependency property, so shouldn't that take care of things?
Here's my XAML (I stripped away the other controls for the sake of readability):
<UserControl x:Class="WindowsApp.FormBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WindowsApp">
<Grid DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:FormBox}}">
<TextBox x:Name="TextForm" Text="{Binding Path=Value, Mode=TwoWay}" />
</Grid>
</UserControl>
And here's the c#, Visual Studio auto-generated it for me so maybe there's something not right about it
public String Value
{
get { return (String)GetValue(ValueProperty); }
set
{
SetValue(ValueProperty, value);
}
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(String), typeof(FormBox));
In other parts of the code, I just assign Value in XAML by just doing Value="{Binding Whatever}"
Any advice or help would be appreciated. Thank you.
You need to set the Text binding's UpdateSourceTrigger to PropertyChanged. The default for TextBox is LostFocus. If this is the only control in a window then there isn't anything else that can receive focus, so a LostFocus event never fires and the binding never updates the source.
<TextBox x:Name="TextForm" Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
这篇关于我的依赖属性仅限于一种方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!