我的依赖属性仅限于一种方式 [英] My Dependency Property is only binding one way

查看:86
本文介绍了我的依赖属性仅限于一种方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的新手,我正在做一些我觉得很简单的事情。我做了一个包含一个文本框的UserControl,并且我想将TextBox.Text属性绑定到我的usercontrol值的依赖属性。文本框正确显示值,如果更改值的值,则文本框将相应更新。但是,如果我更改文本框中的值,则值不会更改以反映它。即使我手动将绑定模式设置为双向,它仍然只绑定一种方式。我把Value作为一个依赖属性,所以不应该照顾这些东西?



这是我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆