2013-06-08 21 views
6

Tôi cần đặt thuộc tính ItemsPanelTemplate của số ListBox dựa trên điều khiển DependencyProperty. Làm cách nào để sử dụng số DataTemplateSelector để thực hiện điều đó?Wpf Cách thay đổi hộp Danh sách ItemspanelTemplate sử dụng Trình kích hoạt

Tôi có một cái gì đó như:

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

đây (ItemsPanelTemplate Selector in wpf?) là liên kết với Câu hỏi tương tự. Dưới đây là mã của tôi nhưng nó không làm việc:

<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 [email protected]}"> 
           </Image> 

           <Grid> 
            <TextBlock Text="{Binding [email protected]}" HorizontalAlignment="Center" FontWeight="Normal" FontSize="13" /> 
           </Grid> 
          </DockPanel> 
         </Border> 
        </Viewbox> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </DockPanel> 
</Window> 

Trong khi chạy mã này tôi nhận được ListBoxVerticalStackStyle 'mà thôi.

Trả lời

8

Khi lỗi nói bạn đang cố gắng đặt Style khi mong đợi một số ItemsPanelTemplateSetter.Value. Xác định các nguồn lực của bạn như ItemPanelTemplate 's hơn ListBoxStyle' s và bạn nên được sắp xếp

Hãy thử một cái gì đó như:

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

Thanks a lot làm việc hoàn toàn tốt đẹp và hơn nữa tôi đã thay đổi ** StaticResource ** ratherthan ** DynamicResource ** và tôi đang mắc kẹt với một vấn đề hơn khi nó aplies ** ListBoxWrapTemplate ** Trên thực tế hình ảnh không nhận được bọc thay vì liên kết của nó Horzontally với ** HorizontalScroll bar **. Tôi không muốn ** Thanh ngang HorizontalScroll ** nhưng hình ảnh đã được Wrapped. – Selva

+0

Có chỉnh sửa nào với thiết kế để hoạt động không ** ListBoxWraptemplate ** ?? xin vui lòng hướng dẫn. Thanx. – Selva

+0

@Selva Tôi nghĩ rằng đó là vì chiều rộng của 'WrapPanel' không ép buộc nó" bọc "như là một sự đoán nhanh, hãy thử thiết lập' Width = "{Binding Path = ActualWidth, RelativeSource = {RelativeSource FindAncestor, AncestorType = {x: Type ListBox}}} "' trên 'WrapPanel' – Viv

1

tôi đã tự hỏi tại sao câu trả lời Viv đã không làm việc trong trường hợp của tôi :-( Đây là những gì tôi đã làm sai:!.

Đừng xác định Appart ItemsPanelTemplate từ <Style.Triggers> Nếu bạn đặt một trong những nguyên nhân <Listbox.ItemPanel></Listbox.ItemPanel> phong cách sẽ không có bất kỳ tác

Arnaud.

ps: không có danh tiếng đủ để chỉ nhận xét ...

Các vấn đề liên quan