我有以下代码,用于将我的用户控件以行和列的形式填充。被填充的用户控件包含按钮、链接、文本框等。当在特定的行/列上按下特定用户控件上的某个按钮时,我需要知道是哪个用户控件被按下了。以下是用于填充行和列中的用户控件的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
的东西,并覆盖模板,例如此处。 - Rachel