WPF TextBlock的在XAML绑定 [英] WPF textblock binding in XAML

查看:154
本文介绍了WPF TextBlock的在XAML绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更新一些现有的WPF代码和我的应用程序有许多这样定义的TextBlocks的:

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

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

在这种情况下,PropertyA是这样定义我的业务类对象的属性:

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

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
    }
}

现在我有一个TextBlock,我需要绑定到其中,你可以看到对象A的属性之一,是MyBusinessObject一个对象。在代码中,我称此为:

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:

MyBusinessObject.ObjectA.PropertyNameHere

我不像其他绑定,PropertyNameHere不是MyBusinessObject的直接财产,而是对对象A的属性。我不知道如何在XAML文本块绑定引用此。谁能告诉我怎么会做这个? !谢谢

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!

推荐答案

<之前,运行文本={结合ObjectA.PropertyNameHere}​​/> 将工作你必须让对象A 本身就是一个属性,因为绑定将只与性能不能下地干活。

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

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

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

这篇关于WPF TextBlock的在XAML绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆