Search code examples
c#wpflistviewcode-behinditemspaneltemplate

ListView how to change ItemsPanel in codebehind instead than in xaml


I had this in my xaml that formatted my listView. That works but I have to add a mode complicated logic so that this has to be applied

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel Orientation = "Horizontal" Width = "250" Background = "{x:Null}"  VerticalAlignment = "Top"></WrapPanel>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

so now I'd need to apply the code above in code behind.

---EDIT for Martino Bordin---

Please tell me what I have misunderstood:

1a. I have defined a style in my listview:

<ListView x:Name="lvPPtab1" Grid.Row="2" FontSize="12" Background="{x:Null}"  BorderBrush="Gainsboro" BorderThickness="5" Margin="10,12.2,10,8.4" VerticalAlignment="Stretch" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" SelectionChanged="ListView_SelectionChanged">
    <ListView.Resources>
        <Style x:Key="ListViewStyle" TargetType="ListView">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate >
                        <WrapPanel Orientation="Horizontal" VerticalAlignment="Top"/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.Resources>

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

2a.I set it in my code behind only when I need it:

<ListView x:Name="lvPPtab1" Grid.Row="2" FontSize="12" Background="{x:Null}"  BorderBrush="Gainsboro" BorderThickness="5" Margin="10,12.2,10,8.4" VerticalAlignment="Stretch" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" SelectionChanged="ListView_SelectionChanged">
    <ListView.Resources>
        <Style x:Key="ListViewStyle" TargetType="ListView">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate >
                        <WrapPanel Orientation="Horizontal" VerticalAlignment="Top"/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.Resources>

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>


lvPPtab1.Style = (Style)this.Resources["ListViewStyle"];

and all what I see is... nothing listView empty.

Then I tried to stick to what you said and so I did that:

1b. in the xaml

<ListView x:Name="lvPPtab1" Grid.Row="2" FontSize="12" Background="{x:Null}"  BorderBrush="Gainsboro" BorderThickness="5" Margin="10,12.2,10,8.4" VerticalAlignment="Stretch" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" SelectionChanged="ListView_SelectionChanged">
<ListView.Resources>
    <ItemsPanelTemplate x:Key="ListViewStyle" >
        <WrapPanel Orientation="Horizontal" VerticalAlignment="Top"></WrapPanel>
    </ItemsPanelTemplate>
</ListView.Resources>
<ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
  1. in the code behind:

    lvPPtab1.ItemsPanel = (ItemsPanelTemplate)this.Resources["ListViewStyle"];

but again nothing! All empty where am I wrong?????


Solution

  • Put the template in the resources dictionary, give a x:Name to your listview, then you can access its properties in the code behind:

    myListView.ItemsPanel
    myListView.ItemsPanel = (ItemsPanelTemplate)this.Resources["MyListViewPanelTemplate"];