4

Expander を使用した典型的な ListView GroupStyle は、おおよそ次のようになります。

<ListView.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander>
                                <Expander.Header>
                                    <TextBlock Text="{Binding Path=Name}" />
                                </Expander.Header>
                                <Expander.Content>
                                    <ItemsPresenter />
                                </Expander.Content>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ListView.GroupStyle>

TextBlock は Name にバインドされています。これは、ListCollectionView の GroupDescriptions コレクションに追加された PropertyGroupDescription に propertyName が渡された GroupItem のオブジェクトの値を生成するようです (おっと):

class MyClass
{
    public int Id { get; set; }
    public int FullName { get; set; }
    public string GroupName { get; set; } // used to group MyClass objects
}

MyView.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));

ListView のグループ見出しに GroupName 以外のプロパティを表示するにはどうすればよいですか? 私が指摘しているのは、GroupName はオブジェクトをグループに分割するのに便利ですが、必ずしもそのグループのヘッダーに使用したい名前ではないということです。

4

2 に答える 2

10

CollectionViewGroup.Name プロパティはオブジェクト型です。これは、GroupName プロパティが文字列である必要がないことを意味します。このようなことを試してください

class MyClass
{
    ...
    public MyGroup GroupName { get; set; } // used to group MyClass objects
}

class MyGroup
{
    public string HeaderText { get; set; }
    // maybe you will need an additional property to make this object
    // unique for grouping (e.g. for different groups with same HeaderText)
}

.

<Expander.Header>
    <TextBlock Text="{Binding Path=Name.HeaderText}" />
</Expander.Header>
于 2013-01-22T01:18:51.713 に答える
0

次のように簡単です:

<TextBlock Text="{Binding Path=Name, StringFormat='MyText: {0}'}" />
于 2013-01-21T21:02:49.727 に答える