I am new to WPF and trying to understand how to use a UniformGrid:
https://docs.microsoft.com/en-us/windows/communitytoolkit/controls/uniformgrid
If no value for
RowsandColumnsare provided, the UniformGrid will create a square layout based on the total number of visible items. If a fixed size is provided forRowsandColumnsthen additional children that can't fit in the number of cells provided won't be displayed.
Based on this text, I thought if I bind a collection of 10 items to a uniform grid and specify 1 row and 3 columns then it would only show 3 items and the other 7 would be cut off.
However, I have built a sample application and with 1 row, 3 columns, and 10 items in my collection, I am getting 4 rows displayed. Here is my sample application:
<Window x:Class="UniformGridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UniformGridTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Name="MyMainWindow">
<ItemsControl ItemsSource="{Binding ActiveList}" Background="Black">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid HorizontalAlignment="Stretch" VerticalAlignment="Center" Rows="1" Columns="3"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding ItemDescription}" Background="Black" Foreground="White"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace UniformGridTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly PlatformSelectorViewModel platformSelectorViewModel = new PlatformSelectorViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = platformSelectorViewModel;
}
}
public class PlatformSelectorViewModel
{
public ObservableCollection<SelectorItem> ActiveList { get; }
public PlatformSelectorViewModel()
{
ActiveList = new ObservableCollection<SelectorItem>();
ActiveList.Add(new SelectorItem() { ItemDescription = "Super Nintendo Entertainment System" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Nintendo Entertainment System" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Sega Genesis" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Sega CD" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Turbo Grafx 16" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Nintendo Gamecube" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Nintendo Wii" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Sony Playstation" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Nintendo PSP" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Nintendo Playstation 2" });
ActiveList.Add(new SelectorItem() { ItemDescription = "Arcade" });
}
}
public class SelectorItem : INotifyPropertyChanged
{
private string itemDescription;
public string ItemDescription
{
get { return itemDescription; }
set
{
itemDescription = value;
PropertyChanged(this, new PropertyChangedEventArgs("ItemDescription"));
}
}
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
}
Based on the documentation on UniformGrid, I had expected that I would get 1 row with 3 items uniformly laid out and the other 7 items would not display. Did I misunderstand the documentation or am I doing something incorrectly that is causing additional rows to display?
