C# コード:
public MainWindow()
{
DataContext = this;
SomeList.Add(new SomeType());
SomeList.Add(new SomeType());
SomeList.Add(new SomeType());
SomeList.Add(new SomeType());
SomeList.Add(new SomeType());
InitializeComponent();
}
//SomeList Observable Collection
private ObservableCollection<SomeType> _someList =
new ObservableCollection<SomeType>();
public ObservableCollection<SomeType> SomeList { get { return _someList; } }
private void UniformGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
var grid = sender as UniformGrid;
if (grid.ActualWidth > 370) grid.Columns = 3;
else if (grid.ActualWidth > 150) grid.Columns = 2;
else grid.Columns = 1;
}
public class SomeType : DependencyObject
{
//Title Dependency Property
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(SomeType),
new UIPropertyMetadata("unset yet"));
}
XAML コード:
<Window.Resources>
<DataTemplate x:Key="SomeTemplate" DataType="{x:Type local:SomeType}">
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="4">
<TextBlock Text="{Binding Title}"/>
</Border>
</DataTemplate>
</Window.Resources>
<ItemsControl
ItemsSource="{Binding SomeList}"
ItemTemplate="{StaticResource SomeTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid SizeChanged="UniformGrid_SizeChanged"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>