【问题标题】:Wpf How to change List box ItemspanelTemplate using TriggerWpf如何使用触发器更改列表框ItemspanelTemplate
【发布时间】:2013-06-08 07:56:26
【问题描述】:

我需要根据控件上的DependencyProperty 设置ListBoxItemsPanelTemplate 属性。我该如何使用DataTemplateSelector 来做到这一点?

我有类似的东西:

<ListBox.ItemsPanel>
   <ItemsPanelTemplate>
       <!-- Here I need to replace with either a StackPanel or a wrap panel-->
   </ItemsPanelTemplate>
</ListBox.ItemsPanel>

这里 (ItemsPanelTemplate Selector in wpf?) 是与类似问题的链接。以下是我的代码,但它不起作用:

<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\00288511851128436163\N12201_0003_003\00000003.tif"/>
                    <Item Name = "HCFA" SNo="2" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/>
                    <Item Name = "HCFA" SNo="3" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/>
                    <Item Name = "HCFA" SNo="4" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/>
                    <Item Name = "HCFA" SNo="5" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/>
                    <Item Name = "HCFA" SNo="6" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/>
                    <Item Name = "HCFA_CC" SNo="7" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000005.tif"/>
                    <Item Name = "FrontPage" SNo="8" Type="GrpBX" Image="C:\Work\00288511851128436163\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>

在运行此代码时,我只得到 ListBoxVerticalStackStyle'。

【问题讨论】:

    标签: wpf listbox itemspaneltemplate


    【解决方案1】:

    正如错误所说,当您期望 ItemsPanelTemplateSetter.Value 时,您尝试设置 Style。将你的资源定义为ItemPanelTemplate's 而不是ListBox Style's,你应该被排序

    尝试类似:

    <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>
      ...
    

    【讨论】:

    • 非常感谢工作得非常好,而且我已经改变了 StaticResource 而不是 DynamicResource 并且当它适用时我又遇到了一个问题 ListBoxWrapTemplate 实际上,图像并没有被包裹,而是与 Horizo​​ntalScroll bar 水平对齐。我不想要这个 Horizo​​ntalScroll bar,但必须包裹图像。
    • 设计是否有任何更正可以工作 ListBoxWraptemplate ?请指导。谢谢。
    • @Selva 我认为这是因为WrapPanel 的宽度不会强制它“换行”作为快速猜测尝试在WrapPanel 上设置Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"
    • 问题在于 ViewBox Stretch="Fill" 如果我给 Stretch="None" 它工作得很好。但我无法找出这个问题的根本原因。可能是由于 LayoutTransform 因为如果我给 Stretch = Fill 每个图像都占用 ListBox 的整个宽度。你这背后有什么想法吗? .非常感谢您的回复。
    【解决方案2】:

    我想知道为什么 Viv 的回答对我来说不起作用 :-( 这是我做错的:

    不要从 &lt;Style.Triggers&gt; 定义 ItemsPanelTemplate appart! 如果您将一个放在&lt;Listbox.ItemPanel&gt;&lt;/Listbox.ItemPanel&gt; 中,样式触发器将不会有任何效果。

    阿诺。

    ps:没有足够的声誉,只能评论......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      相关资源
      最近更新 更多