【问题标题】:Binding disappears on simple user control绑定在简单的用户控件上消失
【发布时间】:2015-09-30 21:11:47
【问题描述】:

我有一个带有单个 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>

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

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

【问题讨论】:

  • 它是Mode=TwoWay。该死的。
  • 你找到了太好了 :)

标签: wpf data-binding user-controls


【解决方案1】:

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

【讨论】:

  • 是否可以指定除了 TwoWay 以外的其他东西作为默认值?
【解决方案2】:

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

 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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2012-03-29
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多