如何为不同的ItemsPanelTemplate

时间:2015-11-05 17:18:42

标签: wpf itemscontrol itemspaneltemplate

我有一个ItemsControl,根据特定条件使用不同的ItemsPanelTemplate。我希望每个ItemsPanelTemplate都有不同的ItemContainerStyle(事实上,我只想要一个模板的ItemsContainerStyle)。我怎样才能做到这一点?这是我正在使用的代码:

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

谢谢, RDV

1 个答案:

答案 0 :(得分:0)

我找到了一种方法,当IsMap为TRUE时,将ItemsContainerStyle与ItemsPanel一起设置。更新后的代码发布在下面:

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