locked
Programmatically Modifying an ItemsPanelTemplate

    Question

  • I have the following ItemsControl in my XAML:
    <ItemsControl x:Name="itmKnotButtons" Grid.Column="0" VerticalAlignment="Center">
    	<ItemsControl.ItemsPanel><ItemsPanelTemplate><VariableSizedWrapGrid MaximumRowsOrColumns="1" Orientation="Horizontal"/></ItemsPanelTemplate></ItemsControl.ItemsPanel>
    	<ItemsControl.ItemTemplate><DataTemplate><RadioButton Style="{StaticResource KnotButtonStyle}" GroupName="KnotButtons" Checked="KnotButton_Checked" Content="{Binding KnotName}" IsChecked="{Binding IsCurrentKnot}"/></DataTemplate></ItemsControl.ItemTemplate>
    </ItemsControl>
    You will notice that the ItemsPanel is a VariableSizedWrapGrid. Anyone that has used a VariableSizedWrapGrid has probably noticed that all cells are the size of the first item. Therefore, I need to set the ItemWidth of the VariableSizedWrapGrid programmatically after calculating the width of the largest item. However, I cannot seem to figure out how to do this. Because this must be done programmatically (I cannot do it in XAML due to the required calculations). Is there any way to modify the VariableSizedWrapGrid from the codebehind? I am also (if necessary) willing to create a new ItemsPanelTemplate in the codebehind, but I have been unable to do this as well. Some of my searches have mentioned stuff about the VisualTreeHelper, but if this is an option, I have been unable to figure out how to use it. Any help on any way to do any of these things would be greatly appreciated. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, November 11, 2014 4:28 AM

All replies

  • My best option/work-a-round would be to create ItemsPanelTemplates in the resources and then from C# assign which resource to use on the ListView.ItemsPanel.

    XAML

    <ItemsPanelTemplate x:Key="HorzIPT" > <StackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> <ItemsPanelTemplate x:Key="VertIPT"> <StackPanel Orientation="Vertical" /> </ItemsPanelTemplate>

    C#

    if(isVert)

    listView.ItemsPanel = app.Resources["VertIPT"] as ItemsPanelTemplate;  

    else

    listView.ItemsPanel = app.Resources["HorzIPT"] as ItemsPanelTemplate;  


    This work-a-round should work for all scenarios that like.
    Wednesday, November 12, 2014 5:43 AM
  • You have obviously misunderstood my problem. There is no problem when my ItemsPanelTemplate is a StackPanel, but my ItemsPanelTemplate is a VariableSizedWrapGrid. As I mention in my original post, the size of ALL cells in a VariableSizedWrapGrid is determined only by the first item. If the first item is not the largest, others will be cropped. Because of this, I need to manually calculate the size of the largest item so that I can set VariableSizedWrapGrid.ItemWidth and VariableSizedWrapGrid.ItemHeight. This must be done at runtime in the codebehind, so I need to access the ItemsPanelTemplate from my code. Any other ideas? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, November 12, 2014 6:52 PM