使用 DependencyProperty 访问 UserControl 代码中的绑定对象
我在通过父 UserControl 上的数据绑定使用 DependencyProperty 设置自定义用户控件的属性时遇到问题。
下面是我的自定义 UserControl 的代码:
public partial class UserEntityControl : UserControl
{
public static readonly DependencyProperty EntityProperty = DependencyProperty.Register("Entity",
typeof(Entity), typeof(UserEntityControl));
public Entity Entity
{
get
{
return (Entity)GetValue(EntityProperty);
}
set
{
SetValue(EntityProperty, value);
}
}
public UserEntityControl()
{
InitializeComponent();
PopulateWithEntities(this.Entity);
}
}
我想要访问后面代码中的 Entity 属性,因为这将根据实体中存储的值动态构建用户控件。我遇到的问题是 Entity 属性从未设置。
以下是我在父用户控件中设置绑定的方法:
<ListBox Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding SearchResults}" x:Name="SearchResults_List">
<ListBox.ItemTemplate>
<DataTemplate>
<!--<views:SearchResult></views:SearchResult>-->
<eb:UserEntityControl Entity="{Binding}" ></eb:UserEntityControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我将 ListBox 的 ItemsSource 设置为 SearchResults,它是实体的可观察集合(与自定义 UserControl 上的实体类型相同)。
我在调试输出窗口中没有收到任何运行时绑定错误。我只是无法设置 Entity 属性的值。有什么想法吗?
I am having trouble setting a property of a custom user control using a DependencyProperty through databinding on the parent UserControl.
Here is the code for my custom UserControl:
public partial class UserEntityControl : UserControl
{
public static readonly DependencyProperty EntityProperty = DependencyProperty.Register("Entity",
typeof(Entity), typeof(UserEntityControl));
public Entity Entity
{
get
{
return (Entity)GetValue(EntityProperty);
}
set
{
SetValue(EntityProperty, value);
}
}
public UserEntityControl()
{
InitializeComponent();
PopulateWithEntities(this.Entity);
}
}
I want access to the Entity property in the code behind because that will dynamically build the user control based on values stored in the Entity. The problem that I am having is that the Entity property is never set.
Here is how I am setting up the binding in the parent user control:
<ListBox Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding SearchResults}" x:Name="SearchResults_List">
<ListBox.ItemTemplate>
<DataTemplate>
<!--<views:SearchResult></views:SearchResult>-->
<eb:UserEntityControl Entity="{Binding}" ></eb:UserEntityControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I am setting the ItemsSource of the ListBox to SearchResults, which is an Observable Collection of Entities (The same type as Entity on the custom UserControl).
I am not getting any runtime binding errors in the debug output window. I just cannot set the value of the Entity property. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正尝试在 c-tor 中使用 Entity 属性,但这还为时过早。 c-tor 将在给出属性值之前被解雇。
您需要做的是将 propertyChanged Event HAndler 添加到 DependencyProperty,如下所示:
You are trying to use the Entity property in the c-tor, which is too soon. the c-tor is going to be fired BEFORE the property value is going to be given.
What u need to do is to add a propertyChanged Event HAndler to the DependencyProperty, like so: