简体   繁体   English

如何将我的代码隐藏绑定更改为XAML绑定

[英]How to change my code-behind binding to XAML binding

I'm new to both C# and WPF. 我是C#和WPF的新手。 I've written a simple program. 我写了一个简单的程序。 I have a class called Counter , that exposes a read-only property Count that starts out at 0, and a public method Increment that simply increments the count by one. 我有一个称为Counter的类,它公开一个以0开始的只读属性Count ,以及一个仅将计数加1的公共方法Increment Counter implements INotifyPropertyChanged . Counter实现INotifyPropertyChanged

I have a Window class (code is below). 我有一个Window类(下面的代码)。 I pass an instance of a Counter object to the constructor and perform a binding. 我将Counter对象的实例传递给构造函数并执行绑定。 The window has a button and a label. 该窗口有一个按钮和一个标签。 The label is bound to the counter's Count property, and the button calls Increment . 标签绑定到计数器的Count属性,并且按钮调用Increment

This all works. 所有这一切。

However, most examples I've seen around the net and MSDN mostly deal with defining the binding in XAML. 但是,我在网上看到的大多数示例和MSDN大多涉及在XAML中定义绑定。 How can I modify my example here to move the binding operation out of code behind and into the markup? 如何在此处修改示例,以将绑定操作移出代码的后面并移入标记中? The Binding property in the Properties window of VS2010 doesn't seem to know how to do what I want. VS2010的“属性”窗口中的Binding属性似乎不知道该怎么做。 Perhaps it's not possible? 也许不可能吗?

One additional question: I don't think this example fits MVVM... My Counter class stands alone, is not tied to a view anywhere except through its property. 另一个问题:我认为此示例不适合MVVM ...我的Counter类是单独存在的,除了通过其属性之外,不与任何视图绑定。 However, the CounterWindow class is holding a reference to it. 但是,CounterWindow类保留对其的引用。 Is this the proper location for this reference? 这是该参考的正确位置吗? I also though that perhaps I should be creating the window, then setting a property (eg CounterObject ) that I would use instead of passing through via the constructor. 我也尽管也许我应该创建窗口,然后设置一个我将使用的属性(例如CounterObject ),而不是通过构造函数进行传递。

public partial class CounterWindow : Window {
    Counter ctr;

    public CounterWindow(Counter ctr) {
        InitializeComponent();

        this.ctr = ctr;
        Binding b = new Binding("Count");
        b.Source = ctr;
        CounterLabel.SetBinding(Label.ContentProperty, b);
    }

    private void IncrementButton_Click(object sender, RoutedEventArgs e) {
        ctr.Increment();
    }
}

Something like this: 像这样:

public CounterWindow(Counter ctr) 
{
    InitializeComponent();
    DataContext = ctr;
}

Markup: 标记:

<Label Content="{Binding Count}" />  

UPD . UPD There's two common approaches in MVVM: view-first and model-first. MVVM中有两种常见方法:视图优先和模型优先。
View first means that you initially create the view, and then view creates view model, which it is bound to. 视图首先意味着您首先创建视图,然后视图创建​​绑定到的视图模型。
Model-first means that first you create the view model, then view model creates its view and passes itself (via constructor or via DataContext property setter) as data context of the view. “模型优先”意味着首先创建视图模型,然后视图模型创建其视图,并将自身(通过构造函数或通过DataContext属性设置器)传递为视图的数据上下文。

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM