C# WPF 中的变量绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 17:39:05  来源:igfitidea点击:

Variable Bindings in WPF

提问by Joseph Sturtevant

I'm creating a UserControl for a rich TreeView (one that has context menus for renaming nodes, adding child nodes, etc.). I want to be able to use this control to manage or navigate any hierarchical data structures I will create. I currently have it working for any data structure that implements the following interface (the interface need not actually be implemented, however, only the presence of these members is required):

我正在为丰富的 TreeView 创建一个 UserControl(一个具有用于重命名节点、添加子节点等的上下文菜单)。我希望能够使用此控件来管理或导航我将创建的任何分层数据结构。我目前让它适用于实现以下接口的任何数据结构(实际上不需要实现接口,但是,只需要这些成员的存在):

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

Then in my UserControl, I use templates to bind my tree to the data structure, like so:

然后在我的 UserControl 中,我使用模板将我的树绑定到数据结构,如下所示:

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

What I would like to do is define the name of each of these members in my RichTreeView, allowing it to adapt to a range of different data structures, like so:

我想做的是在 RichTreeView 中定义每个成员的名称,使其适应一系列不同的数据结构,如下所示:

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

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

Is there any way to expose the Path of a binding inside a UserControl as a public property of that UserControl? Is there some other way to go about solving this problem?

有没有办法将 UserControl 内绑定的 Path 公开为该 UserControl 的公共属性?有没有其他方法可以解决这个问题?

采纳答案by Arcturus

Perhaps this might help:

也许这可能会有所帮助:

Create a new Binding when you set the HeaderProperty property on the Header dependency property:

在 Header 依赖属性上设置 HeaderProperty 属性时创建一个新的 Binding:

Header property is your normal everyday DependencyProperty:

Header 属性是你日常的 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));

and the property of your HeaderProperty works as follows:

并且您的 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 is your normal everyday DependencyProperty, with a method that is invoked as soon as the HeaderProperty changes. So when it changes , it creates a binding on the Header which will bind to the path you set in the HeaderProperty. :)

HeaderProperty 是您日常使用的普通 DependencyProperty,其方法会在 HeaderProperty 更改后立即调用。因此,当它更改时,它会在 Header 上创建一个绑定,该绑定将绑定到您在 HeaderProperty 中设置的路径。:)