1

私はいくつかのリストボックスを持っており、グループエクスパンダーを作成するためにグループスタイルを設定しました。すべてのリストボックスで同じスタイル情報を使用してすべてのエキスパンダーを作成したいのですが、スタイルを使用するたびにヘッダーのコンテンツをカスタムに変更できるようにしたいと思います。

から取り出すことができますが、それでも内容を編集する方法はありますか

現在、私はこれに似た構文を使用しています:

<ListBox x:Name="listBox1" ItemsSource="{Binding}" Height="571" Width="260">

        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True" Style="{StaticResource GroupBoxExpander}">
                                        <Expander.Header>

                                            <Grid Width="190" Background="Yellow">

                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="40" />
                                                    <ColumnDefinition Width="40" />
                                                </Grid.ColumnDefinitions>

                                                <TextBlock Grid.Column="0" Text="Count:" />
                                                <TextBlock Grid.Column="1" Text="{Binding Path=ItemCount}" />

                                            </Grid>

                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter Margin="15,0,0,0"></ItemsPresenter>
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListBox.GroupStyle>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ItemControl1 />
            </DataTemplate>
        </ListBox.ItemTemplate>

    </ListBox>

どんな助けでも大歓迎です!

読んでくれてありがとう

4

2 に答える 2

1

GroupItem は ContentControl であるため、同じ Template を別の ContentTemplate で使用できます。スタイルを次のように分けます。

<Style TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander IsExpanded="True" >
                    <Expander.Header>
                        <ContentPresenter/>
                    </Expander.Header>
                    <Expander.Content>
                        <ItemsPresenter Margin="15,0,0,0"></ItemsPresenter>
                    </Expander.Content>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid Width="190" Background="Yellow">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="40" />
                        <ColumnDefinition Width="40" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="Count:" />
                    <TextBlock Grid.Column="1" Text="{Binding Path=ItemCount}"/>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

ControlTemplate の ContentPresenter は DataTemplate をインスタンス化するため、これは同じ外観になりますが、DataTemplate を置き換えることでヘッダーをカスタマイズできます。ControlTemplate を使用して基本スタイルを作成し、そのスタイルに基づいて、ControlTemplate を再利用するが異なる DataTemplate を持つ他のスタイルを作成できます。

<Style TargetType="{x:Type GroupItem}"
       x:Key="BaseGroupStyle">
    <Setter Property="Template" .../>
</Style>
<Style TargetType="{x:Type GroupItem}"
       BasedOn="{StaticResource BaseGroupStyle}"
       x:Key="CountGroupStyle">
    <Setter Property="ContentTemplate" .../>
</Style>
于 2010-09-08T12:49:55.700 に答える
0

Template (または Style) を Resources セクションに入れ、 x:Shared="false" を設定してみましたか?

于 2010-09-08T12:15:59.477 に答える