Wpf 如何使用触发器更改列表框 ItemspanelTemplate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16997387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wpf How to change List box ItemspanelTemplate using Trigger
提问by Selva
I need to set the ItemsPanelTemplateproperty of a ListBoxbased on a DependencyPropertyon the control. How do I use the DataTemplateSelectorto do that?
我需要根据控件上的 a设置 a 的ItemsPanelTemplate属性。我如何使用来做到这一点?ListBoxDependencyPropertyDataTemplateSelector
I have something like:
我有类似的东西:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<!-- Here I need to replace with either a StackPanel or a wrap panel-->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
Here (ItemsPanelTemplate Selector in wpf?) is link with Similar Question. Below is my code but its not working:
这里(wpf 中的 ItemsPanelTemplate Selector?)是与Similar Question 的链接。下面是我的代码,但它不起作用:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="myXmlDataBase" XPath="/myXmlData">
<x:XData>
<myXmlData xmlns="">
<Item Name = "CoverSheet" SNo="1" Type="GrpBX" Image="C:\Work<ListBox Name="lv"
Background="LightBlue"
FontSize="12"
ItemsSource="{Binding Source={StaticResource myXmlDataBase},
XPath=Item}">
<ListBox.Resources>
<ItemsPanelTemplate x:Key="ListBoxWrapTemplate">
<WrapPanel Width="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemHeight="{Binding (ListView.View).ItemHeight,
RelativeSource={RelativeSource AncestorType=ListView}}"
ItemWidth="{Binding (ListView.View).ItemWidth,
RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="ListBoxHorizontalStackTemplate">
<StackPanel Width="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
Orientation="Horizontal" />
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="ListBoxVerticalStackTemplate">
<StackPanel Width="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.Resources>
<ListBox.Style>
<Style TargetType="ListBox">
<Style.Triggers>
<!-- Your Trigger.. -->
<Trigger Property="Background"
Value="Green">
<Setter Property="ItemsPanel"
Value="{DynamicResource ListBoxVerticalStackTemplate}" />
</Trigger>
<Trigger Property="Background"
Value="LightBlue">
<Setter Property="ItemsPanel"
Value="{DynamicResource ListBoxHorizontalStackTemplate}" />
</Trigger>
<Trigger Property="Background"
Value="LightGreen">
<Setter Property="ItemsPanel"
Value="{DynamicResource ListBoxWrapTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
...
288511851128436163\N12201_0003_003##代码##000003.tif"/>
<Item Name = "HCFA" SNo="2" Type="GrpBX" Image="C:\Work##代码##288511851128436163\N12201_0003_003##代码##000004.tif"/>
<Item Name = "HCFA" SNo="3" Type="GrpBX" Image="C:\Work##代码##288511851128436163\N12201_0003_003##代码##000004.tif"/>
<Item Name = "HCFA" SNo="4" Type="GrpBX" Image="C:\Work##代码##288511851128436163\N12201_0003_003##代码##000004.tif"/>
<Item Name = "HCFA" SNo="5" Type="GrpBX" Image="C:\Work##代码##288511851128436163\N12201_0003_003##代码##000004.tif"/>
<Item Name = "HCFA" SNo="6" Type="GrpBX" Image="C:\Work##代码##288511851128436163\N12201_0003_003##代码##000004.tif"/>
<Item Name = "HCFA_CC" SNo="7" Type="GrpBX" Image="C:\Work##代码##288511851128436163\N12201_0003_003##代码##000005.tif"/>
<Item Name = "FrontPage" SNo="8" Type="GrpBX" Image="C:\Work##代码##288511851128436163\N12201_0003_003\N12201_0003_003I00.tif"/>
</myXmlData>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<DockPanel>
<ListBox Name="lv" ItemsSource="{Binding Source={StaticResource myXmlDataBase},XPath=Item}" FontSize="12" Background="LightGreen" ItemsPanel="{Binding RelativeSource={RelativeSource Self}, Path=Background}">
<ListBox.Resources>
<Style x:Key="ListBoxWrapStyle" TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding FrameworkElement.ActualWidth),RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}" ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxHorizontalStackStyle" TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Width="{Binding (FrameworkElement.ActualWidth),RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxVerticalStackStyle" TargetType="ListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Width="{Binding (FrameworkElement.ActualWidth),RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" Orientation="Vertical" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
<ListBox.Style>
<Style TargetType="ListBox">
<Style.Triggers>
<!-- Your Trigger.. -->
<Trigger Property="Background" Value="Green">
<Setter Property="ItemsPanel" Value="{StaticResource ListBoxVerticalStackStyle}"/>
</Trigger>
<Trigger Property="Background" Value="LightBlue">
<Setter Property="ItemsPanel" Value="{StaticResource ListBoxHorizontalStackStyle}"/>
</Trigger>
<Trigger Property="Background" Value="LightGreen">
<Setter Property="ItemsPanel" Value="{StaticResource ListBoxWrapStyle}"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
<ListBox.ItemTemplate>
<DataTemplate>
<Viewbox Stretch="Fill" HorizontalAlignment="Stretch" >
<Border BorderThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding}" BorderBrush="IndianRed" Margin="0" Height="Auto">
<DockPanel>
<Image
DockPanel.Dock="Top"
Width="150" Margin="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Height="Auto" x:Name="Myimage"
RenderOptions.BitmapScalingMode="HighQuality" Source="{Binding XPath=@Image}">
</Image>
<Grid>
<TextBlock Text="{Binding XPath=@SNo}" HorizontalAlignment="Center" FontWeight="Normal" FontSize="13" />
</Grid>
</DockPanel>
</Border>
</Viewbox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</Window>
While running this code I'm getting ListBoxVerticalStackStyle'only.
运行此代码时,我只得到ListBoxVerticalStackStyle。
回答by Viv
As the error say's your trying to set a Stylewhen it's expecting an ItemsPanelTemplateas Setter.Value. Define your resources as ItemPanelTemplate's than ListBoxStyle's and you should be sorted
正如错误所说,您正在尝试设置 aStyle当它期待一个ItemsPanelTemplateas Setter.Value。将您的资源定义为ItemPanelTemplate's than ListBoxStyle's 并且您应该被排序
Try something like:
尝试类似:
##代码##回答by alphanoch
I was wondering why Viv's answer did not work in my case :-( Here is what I did wrong:
我想知道为什么 Viv 的回答在我的情况下不起作用 :-( 这是我做错的地方:
Do not define the ItemsPanelTemplate appart from the <Style.Triggers>!
If you place one in the <Listbox.ItemPanel></Listbox.ItemPanel>the style triggers won't have any effect.
不要从<Style.Triggers>! 如果您<Listbox.ItemPanel></Listbox.ItemPanel>在样式中放置一个触发器,则不会产生任何效果。
Arnaud.
阿尔诺。
ps: no sufficient reputation to only comment...
ps:没有足够的声誉只能评论......

