我在我的 xaml 中有这个,它格式化了我的 listView。那行得通,但我必须添加一个模式复杂的逻辑,以便必须应用它
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation = "Horizontal" Width = "250" Background = "{x:Null}" VerticalAlignment = "Top"></WrapPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
所以现在我需要在后面的代码中应用上面的代码。
---为马蒂诺·博尔丁编辑---
请告诉我我误解了什么:
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.我仅在需要时将其设置在我的代码后面:
<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。在xml中
<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>
在后面的代码中:
lvPPtab1.ItemsPanel = (ItemsPanelTemplate)this.Resources["ListViewStyle"];
但又什么都没有!都是空的我哪里错了??????