我的收藏似乎很好,因为我能够使用ListView并且它正确地响应添加,但是当我将listview嵌入到UserControl中时却没有。我提供了相关的代码。
我以这种方式创建了一个UserControl派生类:
public partial class MyCtrl: UserControl
{
#region Static Properties
public static readonly DependencyProperty ItemsSourceProperty =
ItemsControl.ItemsSourceProperty.AddOwner(
typeof(MyCtrl),
new PropertyMetadata(MyCtrl.ItemsSourcePropertyChangedCallback));
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static void ItemsSourcePropertyChangedCallback(
DependencyObject controlInstance,
DependencyPropertyChangedEventArgs e)
{
MyCtrl myInstance=(MyCtrl)controlInstance;
myInstance.nestedList.ItemsSource=e.NewValue as IEnumerable;
}
}
像这样的XAML:
<UserControl x:Class="MyCtrl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ListView Name="nestedList" />
</Grid>
</UserControl>
我的消费XAML看起来像这样:
<MyCtrl x:Name="myInstance" ItemsSource="{Binding Path=MyCollection}" />
集合的定义如下:
public static readonly DependencyProperty MyCollectionProperty =
DependencyProperty.Register("MyCollection",
typeof(ObservableCollection<MyObject>),
typeof(ConsumingObject),
new PropertyMetadata(new ObservableCollection<MyObject>());
public ObservableCollection<MyObject> MyCollection
{
get { return (ObservableCollection<MyObject>)this.GetValue(MyCollectionProperty); }
set { this.SetValue(MyCollectionProperty, value); }
}
答案 0 :(得分:0)
如果您的CollectionChanged
是ObservableCollection&lt;
,那么您可能希望在ItemSource
上注册事件处理程序T&gt;
答案 1 :(得分:0)
这个问题可能会有所帮助。
myInstance的DataContext是什么?
代码能够进入这条线吗?
myInstance.nestedList.ItemsSource = e.NewValue as IEnumerable;
如果是,则nested是否为null?