Don't understand how to set grid column of data collection
Posted: 24 Apr 2024, 03:15
I'm having trouble trying to set the grid column of the elements of an ObservableCollection. Everything seems to end up the correct size, but all of them occupy column 0. I've only recently figured out how to do data binding, and it's still somewhat confusing to deal with collections and templates so I'm probably doing something wrong. I've cobbled together a xamltoy that replicates my attempt and problem in a stripped down way.
Code: Select all
<Grid
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" mc:Ignorable="d"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions"
d:DesignWidth="1280" d:DesignHeight="720">
<Grid.Resources>
<StackPanel x:Name="dataModel">
<StackPanel Background="Red"/>
<StackPanel Background="Green"/>
<StackPanel Background="Blue"/>
<StackPanel Background="Yellow"/>
</StackPanel>
<DataTemplate x:Key="PartyTemplate">
<StackPanel x:Name="panel"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}}" Value="0">
<Setter TargetName="panel" Property="Grid.Column" Value="0"/>
<Setter TargetName="panel" Property="Margin" Value="0,0,5,0"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}}" Value="1">
<Setter TargetName="panel" Property="Margin" Value="5,0,5,0"/>
<Setter TargetName="panel" Property="Grid.Column" Value="1"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}}" Value="2">
<Setter TargetName="panel" Property="Margin" Value="5,0,5,0"/>
<Setter TargetName="panel" Property="Grid.Column" Value="2"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}}" Value="3">
<Setter TargetName="panel" Property="Margin" Value="5,0,0,0"/>
<Setter TargetName="panel" Property="Grid.Column" Value="3"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Grid.Resources>
<ItemsControl DataContext="{StaticResource dataModel}" ItemsSource="{Binding Children}" ItemTemplate="{StaticResource PartyTemplate}" AlternationCount="4">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid Margin="10,0,10,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>