【发布时间】:2020-05-08 09:01:28
【问题描述】:
我有一个包含 24 个事物的 ObservableCollection。
private ObservableCollection<Thing> things;
public ObservableCollection<Thing> Things
{
get => things;
set
{
things= value;
RaisePropertyChanged();
}
}
我也有一个选定的东西
private Thing selectedThing;
public Thing SelectedThing
{
get => selectedThing;
set
{
selectedThing= value;
RaisePropertyChanged();
}
}
我需要在网格中显示这些项目。我正在创建一个按钮网格,每个按钮都有一个命令和一个命令参数,允许我从集合中设置选定的事物。
我需要先填充这个网格列。即:
有没有办法在 WPF 中使用 ItemsControl 和统一网格来做到这一点?
<ItemsControl ItemsSource="{Binding Things}" HorizontalAlignment="Center" Margin="20">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" Rows="8" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding ThingPosition}"
Height="30"
Width="80"
Margin="3"
FontSize="8"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.SelectThingCommand}"
CommandParameter="{Binding Path=.}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
【问题讨论】:
-
只需对 Things 中的元素重新排序即可。
标签: c# wpf mvvm itemscontrol