-->

Setting rows of ItemPanelTemplate UniformGrid thro

2019-09-14 00:50发布

问题:

Here's what I have:

<ItemsControl x:Name="InfoGrid">
       <!-- ItemsPanelTemplate -->
       <ItemsControl.ItemsPanel>
               <ItemsPanelTemplate>
                     <UniformGrid Rows="4" Columns="5"                                                  Background="LightGray" />
               </ItemsPanelTemplate>
       </ItemsControl.ItemsPanel>
       <!-- ItemTemplate -->
       <ItemsControl.ItemTemplate>
              <DataTemplate>
                   <Border BorderBrush="Black" BorderThickness="{Binding Path=BorderThickness}">
                   <CheckBox Style="{StaticResource styleCustomCheckBox}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"/>
                   </Border>
              </DataTemplate>
       </ItemsControl.ItemTemplate>

The InfoGrid's itemsource is set to a collection called infoData in the code behind, how can I set the Rows/Columns of this UniformGrid through C# (preferably to variables not contained within infoData)?

I tried binding and using setters but the values don't seem to get applied.

回答1:

You'll need to access the ItemsPanel in the code behind in order to set the columns at runtime. There are a couple different options for this, including navigating the VisualTree. This answer describes the options and the steps in good detail.