ListView 如何在代码隐藏而不是 xaml 中更改 ItemsPanel

ListView how to change ItemsPanel in codebehind instead than in xaml

我的 xaml 中有这个格式化了我的 listView。这行得通,但我必须添加一个模式复杂的逻辑,以便必须应用它

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

所以现在我需要在后面的代码中应用上面的代码。

---编辑 Martino Bordin---

请告诉我我误解了什么:

1a。我在我的列表视图中定义了一个样式:

<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 仅在我需要时在我的代码后面设置它:

<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"];

而我所看到的只是...没有 listView 是空的。

然后我试着坚持你说的,所以我做到了:

1b。在 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. 在后面的代码中:

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

但还是一无所获!全是空我哪里错了?????

将模板放在资源字典中,给你的列表视图一个x:Name,然后你可以在后面的代码中访问它的属性:

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