0

我有一个 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

4

1 回答 1

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>
于 2015-11-05T18:20:51.357 回答