5

GroupStyleを使用しようとしていますが、リスト内のアイテムのヘッダーとしてグループ名を表示する代わりに、ヘッダーのみを表示し、アイテムは表示しません。

リストに特別なスタイルを適用するまでは問題なく動作します。次に、このスタイルはかなり偽物であると判断したので、このエフェクトからUserControlを作成しました。問題は解決しません。

UserControlの目的は、選択したアイテムに消費効果を持たせることです。通常は、展開するといくつかの情報が表示され、さらに多くの情報が表示されます。

UserControl:

<UserControl x:Class="MyProject.CustomUC.ExpandingList"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

    <Grid>
        <ListBox x:Name="List"
                 ItemsSource="{Binding Path=Collection}">
            <ListBox.Template>
                <ControlTemplate TargetType="ListBox">
                    <Border BorderBrush="Blue" 
                                    BorderThickness="1" 
                                    CornerRadius="2" 
                                    Background="White">
                        <ScrollViewer Focusable="False">
                            <StackPanel Margin="2" IsItemsHost="True" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </ListBox.Template>
            <ListBox.GroupStyle>
                <GroupStyle HidesIfEmpty="True">
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}"/>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListBox.GroupStyle>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Border Margin="4" Name="Border" BorderBrush="Blue" BorderThickness="1" CornerRadius="5" Background="LightBlue">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <ContentPresenter Name="Head"
                                                          Margin="4"
                                                      Visibility="Visible"
                                                      ContentTemplate="{Binding ElementName=List, Path=DataContext.HeadTemplate}"/>
                                        <Border Name="Tail" Visibility="Collapsed" Grid.Row="1" BorderThickness="0,1,0,0" BorderBrush="Blue">
                                        <ContentPresenter Margin="4"
                                                      ContentTemplate="{Binding ElementName=List, Path=DataContext.TailTemplate}" />
                                        </Border>
                                    </Grid>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="Tail" Property="Visibility" Value="Visible" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Grid>
</UserControl>

UserControlの使用:

<CustomUC:ExpandingList Collection="{Binding Path=List}">
    <CustomUC:ExpandingList.HeadTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=DisplayName}" />
        </DataTemplate>
    </CustomUC:ExpandingList.HeadTemplate>
    <CustomUC:ExpandingList.TailTemplate>
        <DataTemplate DataType="{x:Type ViewModel:ElementViewModel}">
            <TextBlock Text="{Binding Path=SomeOtherProperties}" />
        </DataTemplate>
    </CustomUC:ExpandingList.TailTemplate>
</CustomUC:ExpandingList>

これは、ExpandingListUserCotnrolから変更した場合に機能します。

<ListView ItemsSource="{Binding Path=List}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=DisplayName}" />
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.GroupStyle>
        <GroupStyle HidesIfEmpty="True">
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

誰が何が間違っているのか知っていますか?

4

1 に答える 1

5

テンプレートで、ListBox次を置き換えます。

<StackPanel Margin="2" IsItemsHost="True" />

<ItemsPresenter Margin="2" />

クラスにはグループ用のItemsPresenter追加のロジックがあり、パネルを直接使用してアイテムを表示している場合は失われます。

于 2011-06-01T16:45:32.800 に答える