简体   繁体   English

如何将我的双向数据绑定从Code-Behind移动到XAML

[英]How to move my 2-way Data Binding from Code-Behind to XAML

Relatively new to WFP and C# (longtime PHP programmer)... WFP和C#(长期PHP程序员)相对较新...

I successfully set up 2-WAY data binding between a TextBox and a Property of an Object. 我成功地在TextBox和Object的Property之间建立了2-WAY数据绑定。 I was able to establish the binding in the Code-Behind, but not do it in the XAML instead. 我能够在Code-Behind中建立绑定,但不能在XAML中建立绑定。

I would like to learn how to do the BINDING in the XAML instead. 我想学习如何在XAML中进行BINDING。 That is, in the example below, how to move the line myTestPanel.DataContext = CURRENT_NETWORK; 也就是说,在下面的示例中,如何移动行myTestPanel.DataContext = CURRENT_NETWORK; from the Code-Behind to the XAML? 从Code-Behind到XAML?

I looked at tutorials, examples, etc., but none helped... They seem to want me to bind the TextBox (or parent object) to a CLASS, not to an OBJECT. 我看了教程,示例等,但没有帮助...他们似乎希望我将TextBox(或父对象)绑定到CLASS,而不是对象。 Some examples suggest binding to a class with suitable constructors. 一些示例建议使用适当的构造函数绑定到类。 But I do not want to do that. 但我不想这样做。 I want to bind to an EXISTING OBJECT that may have existed for some time. 我想绑定到可能已经存在一段时间的“现有对象”。 I can do it just fine in the Code-Behind... But how to do it in the XAML instead? 我可以在Code-Behind中做到这一点......但是如何在XAML中做到这一点呢?

Here's my Code Behind: 这是我的代码背后:

namespace net
{
    public class network
    {
        public int ID { get; set; }   // Property bound to TextBox
    }

   public partial class MainWindow : Window
   {
        network CURRENT_NETWORK = new network();   // My OBJECT (which could have been around for a while)

        public MainWindow()
        {
            InitializeComponent();

            CURRENT_NETWORK.ID = 123;   // The object gets set up...

            // ** Below is the line I want to learn how to move to XAML **
            myTestPanel.DataContext = CURRENT_NETWORK;  // <- CRITICAL LINE
        }
    }
}

and here's a snippet of the XAML: 这是XAML的代码段:

<Window x:Class="net.MainWindow"
    // some lines omitted
    xmlns:local="clr-namespace:net"
>

<DockPanel Name="myTestPanel" >
    <TextBox Text="{Binding Path=ID, Mode=TwoWay}"></TextBox>
</DockPanel>

The 2-way binding works like a dream :) I can see in the TextBox the value in the object's ID Property, and conversely if I edit the number in the TextBox, the object is correctly modified. 双向绑定就像梦一样:)我可以在TextBox中看到对象的ID属性中的值,相反,如果我在TextBox中编辑数字,则对象被正确修改。 (I observed that with a button bringing up a message box, not shown in my code snippet.) (我发现用一个按钮弹出一个消息框,我的代码片段中没有显示。)

BUT how do I move that critical binding between TextBox and EXISTING OBJECT (ie the line myTestPanel.DataContext = CURRENT_NETWORK; ) from the Code-Behind to the XAML? 但是如何将TextBox和EXISTING OBJECT之间的关键绑定(即行myTestPanel.DataContext = CURRENT_NETWORK; )从Code-Behind移动到XAML? THANKS! 谢谢!


PS: why do I want to do the binding in the XAML? PS:为什么要在XAML中进行绑定? Partially just for learning , and partially because I think it'd be more elegant/readable, since the XAML already contains the name of the Property. 部分原因是为了学习 ,部分原因是因为XAML已经包含Property的名称,因此我认为它会更加优雅/可读。 Ie, I'd like to do all the bindings in the XAML rather than some there and some in the Code-Behind. 即,我想在XAML中进行所有绑定,而不是在其中进行某些绑定以及在Code-Behind中进行某些绑定。

Sorry I could not able to add comment, so posting it as answer. 抱歉,我无法添加评论,因此将其发布为答案。 What I am suggesting you is just have separate class as your ViewModel eg NetWorkViewModel and inside your ViewModel create a property of your EXISTING OBJECT type and change the bindings in XAML as well. 我建议你只是有一个单独的类作为你的ViewModel,例如NetWorkViewModel ,在你的ViewModel中创建一个EXISTING OBJECT类型的属性,并改变XAML中的绑定。

public class NetworkViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private Network _model;
    public Network Model
    {
        get { return _model; }
        set
        {
            _model = value;
            OnPropertyChanged("Model");
        }
    }

    private void OnPropertyChanged(string propertyName) 
    {  }
}

public class Network : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private int _id;
    public int Id
    {
        get { return _id; }
        set
        {
            _id = value;
            OnPropertyChanged("Id");
        }
    }

    private void OnPropertyChanged(string propertyName)
    { }
}

In your Xaml 在您的Xaml中

xmlns:vm="clr-namespace:net"
...
<Window.DataContext>
  <vm:NetworkViewModel />
</Window.DataContext>
...
<DockPanel Name="myTestPanel" >
    <TextBox Text="{Binding Path=Model.Id, Mode=TwoWay}"></TextBox>
</DockPanel>

This is one way of doing. 这是一种方法。 Read some MVVM tutorial you will get some other ideas. 阅读一些MVVM教程,您将获得其他一些想法。

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

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