2
我在通过父级UserControl上的数据绑定使用DependencyProperty设置自定义用户控件的属性时遇到问题。使用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属性从未设置。
这里是我如何设置父用户控件绑定:
<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>
我设置列表框到SearchResult所组成的的ItemsSource,这是实体的观察集合(同类型实体在自定义UserControl上)。
我没有在调试输出窗口中收到任何运行时绑定错误。我只是不能设置实体属性的值。有任何想法吗?
轰!爆头!这工作完美。谢谢Elad! – njebert 2011-03-31 19:46:26