C# XAML 中的 WPF 文本块绑定

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

WPF textblock binding in XAML

c#wpfxamldata-bindingtextblock

提问by bmt22033

I'm updating some existing WPF code and my application has a number of textblocks defined like this:

我正在更新一些现有的 WPF 代码,我的应用程序有许多这样定义的文本块:

<TextBlock x:Name="textBlockPropertyA"><Run Text="{Binding PropertyA}"/></TextBlock>

In this case, "PropertyA" is a property of my business class object defined like this:

在这种情况下,“PropertyA”是我的业务类对象的一个​​属性,定义如下:

public class MyBusinessObject : INotifyPropertyChanged
{
    private void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }

    private string _propertyA;
    public string PropertyA
    {
        get { return _propertyA; }
        set
        {
            if (_propertyA == value)
            {
                return;
            }

            _propertyA = value;
            OnPropertyChanged(new PropertyChangedEventArgs("PropertyA"));
        }
    }

    // my business object also contains another object like this
    public SomeOtherObject ObjectA = new SomeOtherObject();

    public MyBusinessObject()
    {
        // constructor
    }
}

Now I have a TextBlock that I need to bind to one of the properties of ObjectA which, as you can see, is an object in MyBusinessObject. In code, I'd refer to this as:

现在我有一个 TextBlock,我需要将它绑定到 ObjectA 的属性之一,如您所见,它是 MyBusinessObject 中的一个对象。在代码中,我将其称为:

MyBusinessObject.ObjectA.PropertyNameHere

Unlike my other bindings, "PropertyNameHere" isn't a direct property of MyBusinessObject but rather a property on ObjectA. I'm not sure how to reference this in a XAML textblock binding. Can anyone tell me how I'd do this? Thanks!

与我的其他绑定不同,“PropertyNameHere”不是 MyBusinessObject 的直接属性,而是 ObjectA 上的属性。我不确定如何在 XAML 文本块绑定中引用它。谁能告诉我我该怎么做?谢谢!

采纳答案by LPL

Before <Run Text="{Binding ObjectA.PropertyNameHere}" />will work you have to make ObjectAitself a property because binding will only work with properties not fields.

<Run Text="{Binding ObjectA.PropertyNameHere}" />工作之前,您必须使ObjectA自己成为一个属性,因为绑定仅适用于属性而不是字段。

// my business object also contains another object like this
public SomeOtherObject ObjectA { get; set; }

public MyBusinessObject()
{
    // constructor
    ObjectA = new SomeOtherObject();
}

回答by ZombieSheep

Try to instantiate ObjectA in the same way as you are doing for PropertyA (Ie. as a property, with a public getter / setter, and calling OnPropertyChanged), then your XAML can be :

尝试以与 PropertyA 相同的方式实例化 ObjectA(即作为属性,使用公共 getter/setter,并调用 OnPropertyChanged),那么您的 XAML 可以是:

<TextBlock Text="{Binding ObjectA.PropertyNameHere}" />

回答by Rajesh Subramanian

You can do a same as you do for PropertyAlike follows,

你可以PropertyA像下面一样做同样的事情,

OnPropertyChanged(new PropertyChangedEventArgs("ObjectA"));

on Designer XAML,

在设计器 XAML 上,

<TextBlock x:Name="ObjectAProperty" Text="{Binding ObjectA.PropertyNameHere}" />

回答by Kevin DiTraglia

You can simply type this:

你可以简单地输入:

<TextBlock Text="{Binding ObjectA.PropertyNameHere"/>

You may want to implement INotifyPropertyChangedwithin your ObjectAclass, as changing properties of the class will not be picked up by the PropertyChangedmethods in your MyBusinessObjectclass.

您可能希望INotifyPropertyChanged在您的ObjectA类中实现,因为PropertyChanged您的MyBusinessObject类中的方法不会获取类的更改属性。

回答by Sheridan

Try this:

尝试这个:

In code:

在代码中:

public MyBusinessObject Instance { get; set; }

Instance = new MyBusinessObject();

In XAML:

在 XAML 中:

<TextBlock Text="{Binding Instance.PropertyNameHere" />