如何将 itemtemplate 中的属性绑定到 itemsource 中的属性?
考虑这个容器:
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 中的属性?或者对于我最终想要实现的目标是否有其他方法?
谢谢!
Consider this container:
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>
{
...
}
Now, I wish to display this data in a listview where the control that displays the item is custom. For that, I'd set the MyItemSource to listview's ItemSource and define a ItemTemplate. However, it seems that I have no access to ItemInfo in the ItemTemplate. This is my 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>
Instead of binding to ItemSource's ItemInfo, it binds to the ItemInfoUserControl properties, which is not what I wish it to do. Is there a way to bind properties in itemtemplate to a property in itemsource ? Or is there an alternative approach to what i'm ultimately trying to accomplish ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,首先您需要使用正确的绑定语法:
而
不仅仅是“
Name =“Binding Name”
”和“Data =“Binding DataValues”
”。请注意在绑定表达式周围添加“{
”和“}
”。只要
Name
和DataValues
是ItemInfoUserControl
中的DependencyProperties
,这可能足以解决您的问题。如果没有,您需要将它们实现为DependencyProperties
以便能够在 XAML 中绑定到它们。请参阅此处,获取有关定义自定义依赖项属性的优秀 MSDN 文章。编辑:另外,刚刚注意到 - 您同时设置了
x:Name
和Name
。来自 MSDN 上的这篇文章:删除
x:Name="itemInfoUserControl"
并查看是否有帮助。Well, first of all you need to use proper binding syntax:
and
Instead of just "
Name = "Binding Name"
" and "Data = "Binding DataValues"
". Note the addition of "{
" and "}
" around your binding expression.This might be enough to solve your problem, as long as
Name
andDataValues
areDependencyProperties
inItemInfoUserControl
. If not, you'll need to implement them asDependencyProperties
in order to be able to bind to them in XAML. See here for a good MSDN article on defining custom dependency properties.Edit: Also, just noticed -- you're setting both
x:Name
andName
. From this article on MSDN:Remove
x:Name="itemInfoUserControl"
and see if that helps.