在 WPF 中绑定到当前数据项的默认索引器的语法?
我有一个列表框,其项目源设置为 DataRow 的 ObservableCollection。假设本例中每个 DataRow 有 5 列。
在列表框的 DataTemplate 中,我有 5 个文本块(每列 1 个)。我的问题是如何绑定到行的索引器以获取列值?
这是我的尝试,但没有显示任何内容,所以我的语法一定是错误的:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=.[0]}" />
<TextBlock Text="{Binding Path=.[1]}" />
<TextBlock Text="{Binding Path=.[2]}" />
<TextBlock Text="{Binding Path=.[3]}" />
<TextBlock Text="{Binding Path=.[4]}" />
</StackPanel>
</DataTemplate>
我知道索引器可以在绑定中使用,因为我已经做了类似的事情:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Collection[0].Name}" />
<TextBlock Text="{Binding Path=Collection[1].Name}" />
<TextBlock Text="{Binding Path=Collection[2].Name}" />
<TextBlock Text="{Binding Path=Collection[3].Name}" />
<TextBlock Text="{Binding Path=Collection[4].Name}" />
</StackPanel>
</DataTemplate>
任何有关纠正我的语法的帮助将不胜感激。
I have a Listbox with an itemssource set to an ObservableCollection of DataRow. Let's say each DataRow has 5 columns for this example.
In the DataTemplate of the ListBox I have 5 textblocks (1 for each column). My question is how can I bind to an indexer of the row to get the columns value?
Here is my attempt but nothing displays so I must have the syntax wrong:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=.[0]}" />
<TextBlock Text="{Binding Path=.[1]}" />
<TextBlock Text="{Binding Path=.[2]}" />
<TextBlock Text="{Binding Path=.[3]}" />
<TextBlock Text="{Binding Path=.[4]}" />
</StackPanel>
</DataTemplate>
I know that indexers can be used in bindings because I've done something like this already:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Collection[0].Name}" />
<TextBlock Text="{Binding Path=Collection[1].Name}" />
<TextBlock Text="{Binding Path=Collection[2].Name}" />
<TextBlock Text="{Binding Path=Collection[3].Name}" />
<TextBlock Text="{Binding Path=Collection[4].Name}" />
</StackPanel>
</DataTemplate>
Any help on correcting my syntax would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的语法应该可以。但是,还必须执行以下操作:
检查您的 ItemsSource。它真的提供了 int 类型的索引器吗?也许您有其他类型的索引器,甚至没有索引。也许它是另一个超出您预期的物体?
Your syntax should do. However, the following must go also:
Check your ItemsSource. Does it really provide an indexer that is of type int? Maybe you have an indexer of another type or even no index. Maybe it's another object than you expect it to be?