简体   繁体   English

如何对不同的ItemsPanelTemplate使用不同的ItemsControl.ItemContainerStyle

[英]How to use different ItemsControl.ItemContainerStyle for different ItemsPanelTemplate

I have an ItemsControl which uses different ItemsPanelTemplate based on certain condition. 我有一个ItemsControl,它根据特定条件使用不同的ItemsPanelTemplate。 I want to have different ItemContainerStyle for each ItemsPanelTemplate (in fact, I want ItemsContainerStyle for only one of the templates). 我希望每个ItemsPanelTemplate具有不同的ItemContainerStyle(实际上,我只希望其中一个模板具有ItemsContainerStyle)。 How can I achieve that? 我该如何实现? Here is the code I am using: 这是我正在使用的代码:

<UserControl.Resources>
    <ItemsPanelTemplate x:Key="UGridItemsPanelTemplate">
      <UniformGrid Name="MyUGrid" Columns="{Binding Columns}" Rows="{Binding Rows}"/>
    </ItemsPanelTemplate>

    <ItemsPanelTemplate x:Key="GridItemsPanelTemplate">
      <Grid Name="MyGrid" Loaded="MyGrid_Loaded"/>
    </ItemsPanelTemplate>
  </UserControl.Resources>

  <Grid>
    <!--ItemList has 1000+ items if IsMap is FALSE; using ItemsConatinerStyle in this case slows the UI down-->
    <ItemsControl  Name="MyPresenter" ItemsSource="{Binding ItemList}" Tag="{Binding IsMap}">
      <ItemsControl.Style>
        <Style TargetType="{x:Type ItemsControl}">
          <Setter Property="ItemsPanel" Value="{StaticResource UGridItemsPanelTemplate}"/>
          <Style.Triggers>
            <Trigger Property="Tag" Value="TRUE">
              <!--I want to use ItemContainerStyle only for this template-->
              <Setter Property="ItemsPanel" Value="{StaticResource GridItemsPanelTemplate}"/>
            </Trigger>
          </Style.Triggers>
        </Style>
      </ItemsControl.Style>

      <!--Use this style only if IsMap is TRUE-->
      <ItemsControl.ItemContainerStyle>
        <Style>
          <Setter Property="Control.Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type ContentControl}">
                <ContentPresenter/>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
          <Setter Property="Grid.Row" Value="{Binding GridRow}"/>
          <Setter Property="Grid.Column" Value="{Binding GridColumn}"/>
        </Style>
      </ItemsControl.ItemContainerStyle>

      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Border x:Name="Border1" Background="{Binding BorderVisible}"
                  BorderThickness="1" Padding="{Binding PaddingVal}">
            <Button Name="ItemButton" Content="{Binding Label}" IsEnabled="{Binding IsButtonEnabled}" CommandParameter="{Binding}"/>
          </Border>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </Grid>
</UserControl>

Thanks, RDV 谢谢,RDV

I found a way, When IsMap is TRUE, set ItemsContainerStyle along with ItemsPanel. 我找到了一种方法,当IsMap为TRUE时,将ItemsContainerStyle和ItemsPanel一起设置。 Updated code is posted below: 更新的代码如下:

  <UserControl.Resources>
    <ItemsPanelTemplate x:Key="UGridItemsPanelTemplate">
      <UniformGrid Name="MyUGrid" Columns="{Binding Columns}" Rows="{Binding Rows}"/>
    </ItemsPanelTemplate>

    <ItemsPanelTemplate x:Key="GridItemsPanelTemplate">
      <Grid Name="MyGrid" Loaded="MyGrid_Loaded"/>
    </ItemsPanelTemplate>

    <Style x:Key="ClusterGridContainerStyle">
      <Setter Property="Control.Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ContentControl}">
            <ContentPresenter/>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
      <Setter Property="Grid.Row" Value="{Binding UnitGridRow}"/>
      <Setter Property="Grid.Column" Value="{Binding UnitGridColumn}"/>
    </Style>

  </UserControl.Resources>

  <Grid>
    <!--ItemList has 1000+ items if IsMap is FALSE-->
    <ItemsControl  Name="MyPresenter" ItemsSource="{Binding ItemList}" Tag="{Binding IsMap}">
      <ItemsControl.Style>
        <Style TargetType="{x:Type ItemsControl}">
          <Setter Property="ItemsPanel" Value="{StaticResource UGridItemsPanelTemplate}"/>
          <Style.Triggers>
            <Trigger Property="Tag" Value="TRUE">
              <!--I want to use ItemContainerStyle only for this template-->
              <Setter Property="ItemsPanel" Value="{StaticResource GridItemsPanelTemplate}"/>
              <Setter Property="ItemContainerStyle" Value="{StaticResource ClusterGridContainerStyle}"/>
            </Trigger>
          </Style.Triggers>
        </Style>
      </ItemsControl.Style>

      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Border x:Name="Border1" Background="{Binding BorderVisible}"
                  BorderThickness="1" Padding="{Binding PaddingVal}">
            <Button Name="ItemButton" Content="{Binding Label}" IsEnabled="{Binding IsButtonEnabled}" CommandParameter="{Binding}"/>
          </Border>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </Grid>
</UserControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为ItemsControl.ItemContainerStyle指定ControlTemplate - Specify ControlTemplate for ItemsControl.ItemContainerStyle WPF ItemsControl.ItemsPanelTemplate作为网格并在不同的列中添加新项目 - Wpf ItemsControl.ItemsPanelTemplate as a Grid and add new items in different columns 当 ItemsControl.ItemContainerStyle 与 ItemsControl.ItemTemplateSelector 一起指定时,后者被忽略 - When ItemsControl.ItemContainerStyle is specified together with ItemsControl.ItemTemplateSelector, the later is ignored 如何为列表框创建不同的ItemsPanelTemplate - How to create different ItemsPanelTemplate for a ListBox 项目源Collection上的WPF ItemsControl.ItemContainerStyle setter属性无法引用该属性 - WPF ItemsControl.ItemContainerStyle setter properties on an items source Collection can't reference the properties ItemsControl:如何在ItemsPanelTemplate中使用FindName来访问Panel - ItemsControl: How To Use FindName within ItemsPanelTemplate to access Panel WPF:如何在ItemsControl中使用两个不同的控件? - WPF: How do I use two different controls in an ItemsControl? 如何在ItemContainerStyle中使用EventToCommand? - how to use EventToCommand in a ItemContainerStyle? 将网格添加到ItemsControl的ItemsPanelTemplate - Add Grid to ItemsPanelTemplate of an ItemsControl 如何将子项添加到 WPF 中 ItemsControl 的 ItemsPanelTemplate? - How can you add Children to an ItemsPanelTemplate of an ItemsControl in WPF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM