繁体   English   中英

将UserControl绑定到其自己的dependencyProperty不起作用

[英]Binding UserControl to its own dependencyProperty doesn't work

我有一个问题,当我将该对象设置为数据绑定时,我无法创建使用自定义对象属性的用户控件。

试着解释一下我的意思是代码。 自定义对象:

public class MyObj
{
    public string Text { get; set; }

    public MyObj(string text)
    {
        Text = text;
    }
}

用户控制代码背后:

/// <summary>
/// Interaction logic for MyControl.xaml
/// </summary>
public partial class MyControl : UserControl
{
    public static readonly DependencyProperty ObjectProperty =
        DependencyProperty.Register("Object", typeof (MyObj), typeof (MyControl), new PropertyMetadata(default(MyObj)));

    public MyObj Object
    {
        get { return (MyObj) GetValue(ObjectProperty); }
        set { SetValue(ObjectProperty, value); }
    }

    public MyControl()
    {
        InitializeComponent();
    }
}

用户控制XAML:

<UserControl x:Class="Test.MyControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<TextBlock Text="{Binding Object.Text}"/>

所以我期望MyControl显示一个TextBlock,文本显示MyObj.Text中的任何字符串;

如果我在代码中添加控件,没有任何绑定,那么这可以正常工作,例如

MyControl myControl = new MyControl(){ Object = new MyObj("Hello World!") };
grid.Children.Add(myControl);

但是,如果我尝试使用数据绑定,则不显示任何内容,这里是MainWindow的代码。

代码隐藏:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private MyObj _Object;
    public MyObj Object
    {
        get { return _Object; }
        set
        {
            _Object = value;
            OnPropertyChanged("Object");
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        Object = new MyObj("HELLO");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML:

任何人都可以指出我正确的方向,我想这与在UserControl上使用相对源绑定有关,但我不确定。

谢谢

我个人从来没有在UserControl上使用相对自我绑定,所以我不确定它是否有效。 您可以尝试设置UserControlx:Name ,并在绑定中使用它。

<UserControl x:Class="Test.MyControl"
             ...
             x:Name="window">
    <TextBlock Text="{Binding ElementName=window, Path=Object.Text}"/>
</UserControl>

请注意,如果数据绑定在运行时无法绑定,您还应在“输出”窗口中看到相关的错误消息。

这已经很久了..但是因为有一种新技术我想在这里发布。

编译时绑定:这是Windows 10引入的一种新类型的绑定。这种绑定具有很多经典绑定的性能优势。

您无需设置任何DataContext页面或控件本身的额外好处是您可以绑定到页面或控件中的任何内容的DataContext

<UserControl x:Class="Test.MyControl"
             ...
             x:Name="window">
    <TextBlock Text="{x:Bind Object.Text}"/>
</UserControl>

但这是否完美如你所想象的那样..不! 不是你猜的。 并且有一个答案。

默认情况下,编译时绑定设置为OneTime ,而不是OneWay的经典绑定。

所以你需要明确地将模式设置为OneWay以确保值始终更新。

<UserControl x:Class="Test.MyControl"
             ...
             x:Name="window">
    <TextBlock Text="{x:Bind Object.Text,Mode=OneWay}"/>
</UserControl>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM