将UserControl绑定到其自身的dependencyProperty不起作用 [英] Binding UserControl to its own dependencyProperty doesn't work
问题描述
我有一个问题,我无法创建一个使用自定义对象的属性的用户控件,当父对象将该对象设置为数据绑定时。
Im having a problem where I can't create a User Control which uses properties of an custom object when the parent has set that object to data bind.
尝试解释一下我的意思是这里的代码。
自定义对象:
To try an explain what I mean here is the code. Custom object:
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中;
So all I expect is for MyControl to display a TextBlock with text showing whatever string is in MyObj.Text;
如果我在代码中添加控件,没有任何绑定,那么这可以工作,例如
If I add the control in code, without any bindings, then this works Okay e.g.
MyControl myControl = new MyControl(){ Object = new MyObj("Hello World!") };
grid.Children.Add(myControl);
但是,如果我尝试使用数据绑定,这不显示任何东西,这里是MainWindow的代码
However if I try to use data binding this doesn't display anything, here is the code for MainWindow.
CodeBehind:
CodeBehind:
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:
XAML:
任何人都可以指出我的方向正确,我想这与在UserControl上使用相对源绑定有关,我不确定。
Could anyone point me in the right direction, I guess it's something to do with using relative source binding on the UserControl but I'm not sure.
谢谢
推荐答案
个人从未在UserControl上使用相对自我约束,所以我不确定它是否有效。您可以尝试设置 UserControl
中的 x:Name
,并在绑定中使用它。
I've personally never used a relative self binding on a UserControl, so I'm unsure if it works. You may try setting the x:Name
of your UserControl
, and use that in the binding.
<UserControl x:Class="Test.MyControl"
...
x:Name="window">
<TextBlock Text="{Binding ElementName=window, Path=Object.Text}"/>
</UserControl>
请注意,如果数据绑定在运行时无法绑定,您还应该看到相关的错误消息在输出窗口中。
Note that if a data-binding fails to bind at runtime, you should also see a related error message in the Output window.
这篇关于将UserControl绑定到其自身的dependencyProperty不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!