質問

リッチな TreeView (ノードの名前変更、子ノードの追加などのためのコンテキスト メニューを持つツリービュー) 用の UserControl を作成しています。このコントロールを使用して、作成する階層データ構造を管理または移動できるようにしたいと考えています。現在、次のインターフェイスを実装する任意のデータ構造で機能します(インターフェイスを実際に実装する必要はありませんが、これらのメンバーの存在のみが必要です)。

interface ITreeItem
{
    string Header { get; set; }
    IEnumerable Children { get; }
}

次に、UserControl で、次のようにテンプレートを使用してツリーをデータ構造にバインドします。

<TextBlock x:Name="HeaderTextBlock" Text="{Binding Path=Header}" />

私がやりたいのは、RichTreeView でこれらの各メンバーの名前を定義し、次のようにさまざまなデータ構造に適応できるようにすることです。

class MyItem
{
    string Name { get; set; }
    ObservableCollection<MyItem> Items;
}

<uc:RichTreeView ItemSource={Binding Source={StaticResource MyItemsProvider}} 
    HeaderProperty="Name" ChildrenProperty="Items" />

UserControl 内のバインディングの Path をその UserControl のパブリック プロパティとして公開する方法はありますか?この問題を解決する他の方法はありますか?

役に立ちましたか?

解決

おそらくこれが役立つかもしれません:

Header 依存関係プロパティで HeaderProperty プロパティを設定するときに、新しい Binding を作成します。

ヘッダー プロパティは、通常の日常的なDependencyPropertyです。

    public string Header
    {
        get { return (string)GetValue(HeaderProperty); }
        set { SetValue(HeaderProperty, value); }
    }

    public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("Header", typeof(string), typeof(ownerclass));

HeaderProperty のプロパティは次のように機能します。

    public static readonly DependencyProperty HeaderPropertyProperty =
        DependencyProperty.Register("HeaderProperty", typeof(string), typeof(ownerclass), new PropertyMetadata(OnHeaderPropertyChanged));

    public string HeaderProperty        
    {
        get { return (string)GetValue(HeaderPropertyProperty); }
        set { SetValue(HeaderPropertyProperty, value); }
    }

   public static void OnHeaderPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        if (args.NewValue != null)
        {
            ownerclass c = (ownerclass) obj;

            Binding b = new Binding();
            b.Path = new PropertyPath(args.NewValue.ToString());
            c.SetBinding(ownerclass.HeaderProperty, b);
        }
    }

HeaderProperty は、HeaderProperty が変更されるとすぐに呼び出されるメソッドを備えた、通常のDependencyProperty です。したがって、それが変更されると、HeaderProperty で設定したパスにバインドされるヘッダー上にバインディングが作成されます。:)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top