绑定在简单的用户控件上消失

Binding disappears on simple user control

我有一个带有单个 DependencyProperty 的简单用户控件。我无法在此 属性 上设置绑定。我没有收到任何异常,但绑定就消失了。

我看不出这里出了什么问题。就是这么简单。

这是我的用户控件:

XAML:

<UserControl x:Class="Test.Controls.SimpleUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         x:Name="ucThis">
        <TextBox Text="{Binding Path=MyString, ElementName=ucThis}" />
</UserControl>

代码:

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

    public string MyString
    {
        get { return (string)GetValue(MyStringProperty); }
        set { SetValue(MyStringProperty, value); }
    }

    public static readonly DependencyProperty MyStringProperty =
        DependencyProperty.Register("MyString", typeof(string),
        typeof(SimpleUserControl), new UIPropertyMetadata("simple user control"));
}

XAML 来自测试应用:

    <StackPanel>
        <testControls:SimpleUserControl MyString="{Binding Path=TestString}"
                                         x:Name="simpleUC" />

        <Label Content="From control" />
        <Border Margin="5"
                BorderBrush="Black"
                BorderThickness="1"
                Visibility="{Binding Path=MyString, ElementName=simpleUC, Converter={StaticResource nullVisConv}}">
            <ContentPresenter Content="{Binding Path=MyString, ElementName=simpleUC}" />
        </Border>
        <TextBlock Text="Value from control is null."
                   Margin="5"
                   Visibility="{Binding Path=MyString, ElementName=simpleUC, Converter={StaticResource nullVisConv}, ConverterParameter={custom:BooleanValue Value=True}}" />

        <Label Content="From binding" />
        <Border Margin="5"
                BorderBrush="Black"
                BorderThickness="1"
                Visibility="{Binding Path=TestString, Converter={StaticResource nullVisConv}}">
            <ContentPresenter Content="{Binding Path=TestString}" />
        </Border>
        <TextBlock Text="Value from binding is null."
                   Margin="5"
                   Visibility="{Binding Path=TestString, Converter={StaticResource nullVisConv}, ConverterParameter={custom:BooleanValue Value=True}}" />

        <TextBox Text="You can set focus here." />
    </StackPanel>

测试应用程序的主要 window 有一个名为 TestString 的 属性,是它自己的 DataContext 并正确实现 INotifyPropertyChangedSimpleUserControl.MyString 按原样更新,但它绑定到 (TestString) 的 属性 不会更新。我已经用 Snoop 检查过这个;我在 SimplerUserControl 上设置的绑定在 运行 时不存在。这里发生了什么?

更新 好的。因此,如果我指定 Mode=TwoWay 绑定有效。那太棒了。谁能向我解释为什么它会这样? 谢谢。

按设计工作:)。 DP 默认为 1-way。就个人而言,我会更改您的 DependencyProperty.Register() 调用以使 DP 默认为双向。这样您就不必在每次使用它时都明确指定双向。您会注意到,当您希望 属性 回写时,框架通常默认使 DP 为双向。只是一个方便。

是的,你需要为依赖提供双向模式属性:

 public string MyString
    {
        get { return (string)GetValue(MyStringProperty); }
        set { SetValue(MyStringProperty, value); }
    }

    public static readonly DependencyProperty MyStringProperty =
 DependencyProperty.Register("MyString", typeof(string),
 typeof(UserControl1), new FrameworkPropertyMetadata("simple user control", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));