簡體   English   中英

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

[英]How to use different ItemsControl.ItemContainerStyle for different 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

我找到了一種方法,當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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM