Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XAML中的WPF文本块绑定_C#_Wpf_Xaml_Data Binding_Textblock - Fatal编程技术网

C# XAML中的WPF文本块绑定

C# XAML中的WPF文本块绑定,c#,wpf,xaml,data-binding,textblock,C#,Wpf,Xaml,Data Binding,Textblock,我正在更新一些现有的WPF代码,我的应用程序有许多文本块,定义如下: <TextBlock x:Name="textBlockPropertyA"><Run Text="{Binding PropertyA}"/></TextBlock> public class MyBusinessObject : INotifyPropertyChanged { private void OnPropertyChanged(PropertyChangedEven

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

<TextBlock x:Name="textBlockPropertyA"><Run Text="{Binding PropertyA}"/></TextBlock>
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,我需要绑定到ObjectA的一个属性,正如您所看到的,它是MyBusinessObject中的一个对象。在代码中,我将其称为:

MyBusinessObject.ObjectA.PropertyNameHere

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

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

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

您可以像对
属性执行相同的操作a
如下所示

OnPropertyChanged(new PropertyChangedEventArgs("ObjectA"));
在设计器XAML上

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

您只需键入以下内容:

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

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

请尝试以下操作:

代码:

public MyBusinessObject Instance { get; set; }

Instance = new MyBusinessObject();
在XAML中:

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

起作用之前,您必须将
对象a
本身设置为属性,因为绑定只对属性而不是字段起作用

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

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

是的,我知道,在propery,他没有调用属性更改事件。。。因此,首先将其包括在内,并添加了xaml