【问题标题】:Binding to DependencyProperty with no result绑定到 DependencyProperty 没有结果
【发布时间】:2012-10-11 00:34:58
【问题描述】:

我在绑定到新控件的依赖属性时遇到问题。
我决定编写一些测试来检查这个问题。

从 TextBox.Text 绑定到另一个 TextBox.Text

XAML 代码:

<TextBox Name="Test" Text="{Binding ElementName=Test2, Path=Text, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Name="Test2" Grid.Row="2" />

结果很好 - 当我在第一个 TextBox 中写东西时 -> 第二个 TextBox 正在更新(反之亦然)。

我创建了新控件 -> 例如具有依赖属性“SuperValue”的“SuperTextBox”。

控制 XAML 代码:

<UserControl x:Class="WpfApplication2.SuperTextBox"
             ...
             Name="Root">
    <TextBox Text="{Binding SuperValue, ElementName=Root, UpdateSourceTrigger=PropertyChanged}" />
</UserControl>

后面的代码:

public partial class SuperTextBox : UserControl
{
    public SuperTextBox()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty SuperValueProperty = DependencyProperty.Register(
        "SuperValue",
        typeof(string),
        typeof(SuperTextBox),
        new FrameworkPropertyMetadata(string.Empty)
    );

    public string SuperValue
    {
        get { return (string)GetValue(SuperValueProperty); }
        set { SetValue(SuperValueProperty, value); }
    }
}

好的,现在开始测试!

从 TextBox.Text 绑定到 SuperTextBox.SuperValue

    <TextBox x:Name="Test1" Text="{Binding ElementName=Test2, Path=SuperValue, UpdateSourceTrigger=PropertyChanged}" />
    <local:SuperTextBox x:Name="Test2" Grid.Row="2"/>

测试也是正确的! 当我在 TextBox 中写东西时,SuperTextBox 正在更新。 当我在 SuperTextBox 中写作时,TextBox 正在更新。 一切正常!

现在一个问题:
从 SuperTextBox.SuperValue 绑定到 TextBox.Text

    <TextBox x:Name="Test1"/>
    <local:SuperTextBox x:Name="Test2" SuperValue="{Binding ElementName=Test1, Path=Text, UpdateSourceTrigger=PropertyChanged}" Grid.Row="2"/>

在这种情况下,当我在 SuperTextBox 中写东西时,TextBox 没有更新!

我该如何解决这个问题?

PS:问题很长,很抱歉,但我试着准确描述我的问题。

【问题讨论】:

    标签: c# wpf binding user-controls dependency-properties


    【解决方案1】:

    一个有效而另一个无效的原因是因为TextBoxText 依赖属性被定义为默认绑定TwoWay,而您的依赖属性SuperValue 不是。如果除了源更新目标之外,还希望目标更新源,则需要使用双向绑定。

    要解决此问题,您可以将 FrameworkPropertyMetadataOptions.BindsTwoWayByDefault 添加到 SuperValue's 元数据,如下所示:

    public static readonly DependencyProperty SuperValueProperty = DependencyProperty.Register(
        "SuperValue",
        typeof(string),
        typeof(SuperTextBox),
        new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
    );
    

    【讨论】:

    • 哦,又一个案例还是不行。你能检查新版本的问题吗 - 我添加了“Edit1”部分。
    • 对不起,但我认为这两个问题非常相似,不需要创建新的“主题”。我删除了“已接受的答案”,因为它会造成“主题已结束”的错觉。但是好吧,对不起。
    【解决方案2】:

    因为在前两种情况下Test1 知道何时需要更新自己,但在第三种情况下不知道。只有Test2 知道在第三种情况下应该何时更新。这就是为什么在第三种情况下需要TwoWay 模式。

    编辑

    • 第一个案例在幕后工作,xaml 挂钩 由PropertyDescriptor 公开的AddValueChanged 事件。为了 它工作的原因请参阅此链接here

    【讨论】:

    • 但在第一种情况下,我可以在第二个框中写入(其中没有任何有关数据绑定的信息)并且第一个将被更新。
    • 更新了答案。请检查。
    【解决方案3】:

    将绑定模式更改为双向。

    【讨论】:

    • 谢谢!这是作品!但是,为什么在第一种和第二种情况下,代码在没有“Mode=TwoWay”的情况下工作?
    • msdn.microsoft.com/en-us/library/… 这是一个解释... TextBox 文本“默认”为 BindingMode.TwoWay。您的财产没有。
    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 2011-07-25
    • 2013-06-03
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多