C# 绑定源是带有属性路径的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/267603/
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-08-03 20:33:59  来源:igfitidea点击:

Binding source is string with path to property

c#wpfbinding

提问by Nick

I am unsure if this is even possible, but I thought I would ask. First off, for my purposes, I require this to work in the C# portion and not the XAML part. This is what I have and it works:

我不确定这是否可能,但我想我会问。首先,出于我的目的,我需要它在 C# 部分而不是 XAML 部分中工作。这就是我所拥有的并且它有效:

public partial class MyClass1 : Window
{
     public MyClass2 MyClass2Object { get; set; }

     public MyClass1()
     {
          InitializeComponent();
          MyClass2Object = new MyClass2();
          Binding binding = new Binding();
          binding.Source = MyClass2Object;
          binding.Path = new PropertyPath("StringVar");
          TextBoxFromXaml.SetBinding(TextBox.TextProperty, binding);
     }
}
public class MyClass2
{
     public string StringVar { get; set; }

     public MyClass2()
     {
          StringVar = "My String Here";
     }
}

And this will bind to my StringVar property exactly how I would like it to. However, my question comes with what if I have the literal string "MyClass2Object.StringVar" when setting the binding source. I realize I can use the split function to separate "MyClass2Object" and "StringVar" from the longer string. I can then just replace the new PropertyPath line with the the second result from the split. However, how would I replace the binding.Source line according to the first result from the split. If this is possible, I would be able to pass a string like "MyClass2Object.StringVar" and have the TextBox's Text property bind to that property or if I pass a string like "AnotherClassObject.StringProperty" and have the TextBox's Text property bind to the StringProperty property of the object instantiated in the variable with name AnotherClassObject. I hope I am making sense.

这将按照我希望的方式绑定到我的 StringVar 属性。但是,我的问题是如果我在设置绑定源时有文字字符串“MyClass2Object.StringVar”怎么办。我意识到我可以使用 split 函数将“MyClass2Object”和“StringVar”与较长的字符串分开。然后我可以用拆分的第二个结果替换新的 PropertyPath 行。但是,我将如何根据拆分的第一个结果替换 binding.Source 行。如果这是可能的,我将能够传递像“MyClass2Object.StringVar”这样的字符串并将 TextBox 的 Text 属性绑定到该属性,或者如果我传递一个像“AnotherClassObject.StringProperty”这样的字符串并具有 TextBox' ■ Text 属性绑定到在名为AnotherClassObject 的变量中实例化的对象的StringProperty 属性。我希望我说得有道理。

采纳答案by Todd White

It sounds like you want the PropertyPath to be "Property.Property" which will work, but for the binding to work it needs a source object for the first Property. The two options that I'm aware of are DataContextor a Source.

听起来您希望 PropertyPath 为“Property.Property”,这将起作用,但是要使绑定起作用,它需要第一个 Property 的源对象。我知道的两个选项是DataContextSource

With your sample code the other alternative is:

使用您的示例代码,另一种选择是:

public partial class Window1 : Window
{
    public MyClass2 MyClass2Object { get; set; }
    public Window1()
    {
        // use data context instead of source
        DataContext = this;

        InitializeComponent();

        MyClass2Object = new MyClass2();
        Binding binding = new Binding();
        binding.Path = new PropertyPath("MyClass2Object.StringVar");
        TextBoxFromXaml.SetBinding(TextBox.TextProperty, binding);
    }
}

public class MyClass2
{
    public string StringVar { get; set; }
    public MyClass2()
    {
        StringVar = "My String Here";
    }
}