【发布时间】:2013-08-29 16:37:51
【问题描述】:
我有以下代码以行和列的形式填充我的用户控件。正在填充的用户控件包含按钮、链接、文本框等。当在特定行/列中的特定用户控件上按下某个按钮时,我需要知道该按钮被按下的是哪个用户控件。这是在行和列中填充用户控件的 XAML
<ItemsControl ItemsSource="{Binding Templates}" Width="{Binding GridWidth}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="{Binding NumColumns}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Column" Value="{Binding ColumnIndex}" />
<Setter Property="Grid.Row" Value="{Binding RowIndex}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
</ItemsControl>
模板基本上是在行/列中填充的用户控件的集合。 最好我想在 ViewModel 中执行此操作,但现在代码中的解决方案也可以。
【问题讨论】:
-
ItemsControl不跟踪所选项目。如果您想要这种行为,请使用ListBox之类的内容并覆盖模板,例如 this