【发布时间】:2011-05-22 21:31:34
【问题描述】:
考虑这个容器:
public class ItemInfo : DependencyObject
{
public string Name { get; set; }
public ObservableCollection<SomeDataItem> DataValues { get; set; }
...
Dependency object registration and event handling
...
}
public class MyItemSource : ObservableCollection<ItemInfo>
{
...
}
现在,我希望在列表视图中显示这些数据,其中显示项目的控件是自定义的。为此,我将 MyItemSource 设置为 listview 的 ItemSource 并定义一个 ItemTemplate。但是,似乎我无法访问 ItemTemplate 中的 ItemInfo。这是我的 XAML:
<Grid>
<ListBox ItemsSource="{StaticResource MyStaticDataSource}"
Grid.IsSharedSizeScope="True">
<ListBox.ItemTemplate>
<DataTemplate>
<local:ItemInfoUserControl x:Name="itemInfoUserControl"
Name = "{Binding Name}" <--- this doesn't work
Data = "{Binding DataValues}" <--- this doesn't work
Width="300" Height="200"
Grid.Row="1">
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
它不是绑定到 ItemSource 的 ItemInfo,而是绑定到 ItemInfoUserControl 属性,这不是我希望它做的。有没有办法将 itemtemplate 中的属性绑定到 itemsource 中的属性?或者我最终想要完成的事情有替代方法吗?
谢谢!
【问题讨论】:
-
我认为这可能是因为您试图同时设置
x:Name和Name。请参阅下面的编辑。
标签: wpf listview wpf-controls binding observablecollection