如何将我的2路数据从$ C $绑定C-背后XAML [英] How to move my 2-way Data Binding from Code-Behind to XAML

查看:165
本文介绍了如何将我的2路数据从$ C $绑定C-背后XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相对较新的世界粮食计划署和C#(长期PHP程序员)...

我成功地建立了2-WAY数据一个文本框和一个对象的属性之间的绑定。我是能够建立在code-后面的约束力,但不这样做在XAML来代替。

我想学习如何做绑定在XAML来代替。也就是说,在下面的例子中,如何招行 myTestPanel.DataContext = CURRENT_NETWORK; 从code-背后的XAML

我看了看教程,范例等,但没有帮助......他们好像要我到文本框(或父对象)绑定到一个类,而不是一个对象。一些例子建议结合与合适的构造一类。但我并不想这样做。我要绑定可能已存在一段时间的现有对象。我所能做的只是在code-背后...但如何做到这一点的XAML呢?

罚款

下面是我的code背后:

 命名空间网
{
    公共类网络
    {
        公众诠释ID {搞定;组; } //属性必然的TextBox
    }   公共部分类主窗口:窗口
   {
        网络CURRENT_NETWORK =新的网络(); //我的目标(这本来是一段时间了)        公共主窗口()
        {
            的InitializeComponent();            CURRENT_NETWORK.ID = 123; //该对象被设置...            // **下面是我要学习如何移动到XAML行**
            myTestPanel.DataContext = CURRENT_NETWORK; //< - 临界线
        }
    }
}

和这里的XAML的一个片段:

 <窗​​口x:类=net.MainWindow
    //一些行省略
    XMLNS:地方=CLR的命名空间:网
>< D​​ockPanel中NAME =myTestPanel>
    <文本框的文本={绑定路径= ID,模式=双向}>< /文本框>
< / DockPanel中>

2路绑定工作,像一个梦:)我可以在文本框中看到该对象的ID属性值,反之如果我在TextBox编辑号码,对象正确修改。 (我观察到一个按钮带来了一个消息框,而不​​是在我的code片段所示)。

但我怎么动关键TextBox和现有的对象之间的结合(即行 myTestPanel.DataContext = CURRENT_NETWORK; )从code-背后的XAML? 谢谢!


PS:为什么我想要做的XAML的约束力?部分的只是为了学习的,部分是因为我觉得这是更优雅/可读性,因为XAML已经包含了属性的名称。即,我想这样做在XAML中的所有绑定,而不是一些有有的在code-落后。


解决方案

很抱歉我不能添加评论,所以张贴的答案。
我的建议,你只是有单独的类作为您的视图模型例如 NetWorkViewModel 和您的视图模型中创建您的现有对象类型的属性并更改XAML绑定为好。

 公共类NetworkViewModel:INotifyPropertyChanged的
{
    公共事件PropertyChangedEventHandler的PropertyChanged;
    专用网络_model;
    公共网络模型
    {
        {返回_model; }
        组
        {
            _model =价值;
            OnPropertyChanged(模式);
        }
    }    私人无效OnPropertyChanged(字符串propertyName的)
    {}
}公共类网络:INotifyPropertyChanged的
{
    公共事件PropertyChangedEventHandler的PropertyChanged;
    私人诠释_id;
    公众诠释标识
    {
        {返回_id; }
        组
        {
            _id =价值;
            OnPropertyChanged(ID);
        }
    }    私人无效OnPropertyChanged(字符串propertyName的)
    {}
}

在XAML中

 的xmlns:VM =CLR的命名空间:网
...
< Window.DataContext>
  < VM:NetworkViewModel />
< /Window.DataContext>
...
< D​​ockPanel中NAME =myTestPanel>
    <文本框的文本={绑定路径= Model.Id,模式=双向}>< /文本框>
< / DockPanel中>

这是做的一种方式。看了一些MVVM教程中,你会得到一些其他的想法。

Relatively new to WFP and C# (longtime PHP programmer)...

I successfully set up 2-WAY data binding between a TextBox and a Property of an Object. I was able to establish the binding in the Code-Behind, but not do it in the XAML instead.

I would like to learn how to do the BINDING in the XAML instead. That is, in the example below, how to move the line myTestPanel.DataContext = CURRENT_NETWORK; from the Code-Behind to the 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. 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?

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:

<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. (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 (i.e. the line myTestPanel.DataContext = CURRENT_NETWORK;) from the Code-Behind to the XAML? THANKS!


PS: why do I want to do the binding in the 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. I.e., I'd like to do all the bindings in the XAML rather than some there and some in the 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 e.g. NetWorkViewModel and inside your ViewModel create a property of your EXISTING OBJECT type and change the bindings in XAML as well.

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

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.

这篇关于如何将我的2路数据从$ C $绑定C-背后XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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