当this.DataContext = this时,为什么绑定到我的类的实例不起作用

时间:2015-01-22 23:34:33

标签: c# wpf xaml binding datacontext

我有一个类,它定义了一个预先配置的套接字以及远程访问和控制特定设备所需的所有方法。该类的一部分包括一个对象的实例,该对象保存设备各个方面的当前状态。对象中的每个项目都使用INotifyPropertyUpdate报告更新。当我将它插入我的测试程序时,所有方法都被调用并正确执行,但我似乎能够获得要在UI中显示的状态更新的唯一方法是将DataContext设置为&#34 ;电流和#34;类实例中的对象。如果我将DataContext设置为类的实例或UI,我将停止在UI中获取更新。我希望能够将UI用作DataContext,然后使用{Binding Path=InstanceOfMyClass.Current.StatusItemA}

在XAML中绑定

有关课程的相关部分:

public MyClass : Socket, INotifyPropertyChanged // INotifyPropertyChanged is also used to notify changes in other parts of the class
{
    public MyClass : base(//socket configuration info here)
    {}

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private CurrentStatusObject _current = new CurrentStatusObject();
    public CurrentStatusObject Current
    {
        get { return _current; }
        set
        {
            if (_current != value)
            {
                _current = value;
                NotifyPropertyChanged();
            }
        }
    }

    // other methods and properties etc.
}

// this is the Current status object
public class CurrentStatusObject : object, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private string _statusItemA;

    public string StatusItemA
    {
        get { return _statusItemA; }
        set
        {
            if (_statusItemA != value)
            {
                _statusItemA = value;
                NotifyPropertyChanged(); // not necessary to pass property name because of [CallerMemberName]
            }
        }
    }

这有效:

C#

this.DataContext = this.InstanceOfMyClass.Current;

XAML

<Label Content="{Binding Path=StatusItemA, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>

这不起作用,但我想要:

C#

this.DataContext = this;

XAML

<Label Content="{Binding Path=InstanceOfMyClass.Current.StatusItemA, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>

这也不是:

C#

this.DataContext = this.InstanceOfMyClass;

XAML

<Label Content="{Binding Path=Current.StatusItemA, Mode=OneWay, UpdateSourceTrigger}"/>

我在搜索时没有看到任何答案,但有时我的研究技巧让我失望。任何帮助,将不胜感激。我喜欢学习新的编码方式。这是我的第一个c#或wpf项目。我之前的所有项目都是WinForms中的vb.net,所以我对学习曲线略有不足。我想了解实现此项目目标的正确方法,此时只需完成UI。

CurrentStatusObject在内部通知更改,并且确实有效。问题是,如果我将UI的DataContext设置为该对象,则更改仅反映在用户界面中。我希望能够将DataContext设置为包含更广泛的范围。如果我可以使用MyClass的实例作为DataContext,我会很高兴,但现在不能正常工作。

问题是为什么?以及如何使用它(使用正确的做法)?

2 个答案:

答案 0 :(得分:2)

我认为你有一个错字......

public _Current Current = new _Current();

但如果是这样,这是一个领域,而不是一个属性。如果将其更改为此,则绑定可能会起作用

private _Current _current = new _Current();
public _Current Current
{
    get 
    {
        return _current;
     }
} 

B.T.W:使用下划线作为班级名称的一部分是不标准的。删除它应该是你所需要的

答案 1 :(得分:1)

您只能绑定到属性,永远不会绑定到字段。

InstanceOfMyClassCurrent需要先声明为属性才能绑定到它(使DataContext = this工作)。

另外,MVVM规定您不应该使用View代码隐藏作为视图模型。你应该有一个单独的类。

相关问题