1

私には2つのクラスがあります

public class BookItem
{
    public string BookID            { get; set; }
    public string ItemID            { get; set; }
    public Item Item                { get; set; }   
    public ItemType Type            { get; set; }
    public string ParentID          { get; set; }
    public string BoxID             { get; set; }
    public string StyleID           { get; set; }
    public string NotesID           { get; set; }
    public string Code_XAML         { get; set; }
    public string Description_XAML      { get; set; }
    public CompositeCollection SubItems { get; set; }
}

public class Item : ClaunchBaseClass
{
    public string ItemID        { get; set; }
    public int    Type          { get; set; }
    public string Code          { get; set; }
    public string Description   { get; set; }
    private BookList _books = new BookList();
    public BookList Books       { get {return _books;} set { _books = value; }}
}

次のXAMLを作成しました。

<pre>    
<TreeView Name="tvList" Grid.Row="2" MouseDoubleClick="tvList_MouseDoubleClick">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate DataType="x:Type j:BookItem" ItemsSource="{Binding SubMenu}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Item.Code}" Grid.Column="0" />
                <TextBlock Text="{Binding Item.Description}" Grid.Column="1"/>
            </Grid>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>
<code>

このXAMLは、ツリービューアイテムをブックアイテムのコレクションにバインドし、Itemサブクラスの説明とコードを表示します。ツリービューは正しく入力されて表示されますが、今度はItem.CodeまたはItem.Descriptionのいずれかでツリービューを並べ替えたいので、次のように試しました。結果がありません:

<pre> 
var bookItemsSort = CollectionViewSource.GetDefaultView(_bookItemList) as ListCollectionView;
tvList.ItemsSource = _bookItemList;         //bind the book items to the treeview
bookItemsSort.SortDescriptions.Clear();
bookItemsSort.SortDescriptions.Add(new SortDescription(sort, Ascending));
<code>

このコードは他のツリービューでも正しく機能するので、サブクラスへのバインドに問題があると推測できます。

4

3 に答える 3

2

ここでの回答は私の質問に対する部分的な回答を提供しましたが、どれも私が必要とする回答を提供しませんでした。

この問題の最も賢明な解決策は、このオブジェクトタイプ用に独自のオブジェクト比較プログラムを作成し、基になるリストを並べ替えてから、新しいリストをツリービューに再バインドすることでした。これにより、他の方法では機能させることができなかったネストされたレベルでサブレーザーを比較することができました:)

于 2012-04-23T13:44:38.797 に答える
0

各サブリストのデフォルトのビューを取得し、それにCollectionViewSourceの並べ替えを適用する必要があります。投稿したコードは、トップレベルのアイテムにのみ影響します。

于 2012-04-16T17:00:30.283 に答える
0

TreeView.ItemsSourceをDefaultViewにバインドします。SortDescriptionsは、データリストを変更せず、ビューのみを変更します。

tvList.ItemsSource = bookItemsSort;

Bea Stollnitzブログを参照してください:階層を並べ替えるにはどうすればよいですか?

于 2012-04-16T16:57:16.370 に答える