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

时间:2016-04-13 07:35:14

标签: c# wpf listview code-behind itemspaneltemplate

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

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

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

---为Martino Bordin编辑---

请告诉我我误解了什么:

1a上。我在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。我只在需要时将其设置在我的代码中:

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

  2. 但又一无所获!所有的空虚我错了?????

1 个答案:

答案 0 :(得分:3)

将模板放入资源字典中,为列表视图提供 x:Name ,然后您可以在后面的代码中访问其属性:

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