gpt4 book ai didi

WPF 绑定(bind)到数据上下文

转载 作者:行者123 更新时间:2023-12-03 10:59:51 26 4
gpt4 key购买 nike

为什么 XAML 绑定(bind)在我的 View 模型中成功:

    private string _testStr = "Test";
public string TestStr
{
get { return _testStr; }
}

但不是这样:
    public string TestStr = "Test";

我错过了什么吗?

谢谢。

最佳答案

因为字段不是有效的 binding source :

公共(public)语言运行时 (CLR) 对象

You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object. The binding engine uses CLR reflection to get the values of the properties. Alternatively, objects that implement ICustomTypeDescriptor or have a registered TypeDescriptionProvider also work with the binding engine.



动态对象

You can bind to available properties and indexers of an object that implements the IDynamicMetaObjectProvider interface. If you can access the member in code, you can bind to it.



ADO.NET 对象

You can bind to ADO.NET objects, such as DataTable. The ADO.NET DataView implements the IBindingList interface, which provides change notifications that the binding engine listens for.



XML 对象

You can bind to and run XPath queries on an XmlNode, XmlDocument, or XmlElement. A convenient way to access XML data that is the binding source in markup is to use an XmlDataProvider object. You can also bind to an XElement or XDocument, or bind to the results of queries run on objects of these types by using LINQ to XML. A convenient way to use LINQ to XML to access XML data that is the binding source in markup is to use an ObjectDataProvider object.



DependencyObject 对象

You can bind to dependency properties of any DependencyObject.

关于WPF 绑定(bind)到数据上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17727867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com