在 WPF 中将 TextBox 绑定到对象的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41827885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:59:42  来源:igfitidea点击:

Bind a TextBox to an object's property in WPF

c#wpfbinding

提问by user3567816

I know you would say this question is duplicated, but I haven't found any working solution. So an easy scenario:

我知道你会说这个问题是重复的,但我还没有找到任何可行的解决方案。所以一个简单的场景:

I have a textbox and a button:

我有一个文本框和一个按钮:

<TextBox x:Name="textbox" Text="{Binding st.SomeText}">
<Button Click="Button_Click">Push</Button>

I have a class named Something:

我有一个名为Something的类:

public class Something
{
    public string SomeText { get; set; }
}

I have an object in the MainWindow class:

我在 MainWindow 类中有一个对象:

public partial class MainWindow : Window
{
    Something st;

    public MainWindow()
    {
        InitializeComponent();
        st = new Something();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(st.SomeText);
    }
}

So I want to type something to the textbox, press the button and get a message contains the textbox's text, but I keep getting empty messages instead. What should I modify?

所以我想在文本框中输入一些东西,按下按钮并收到一条包含文本框文本的消息,但我一直收到空消息。我应该修改什么?

A lot of 'solution' said to use a Stackpanel or something and set the DataContext, but it didn't work either.

很多“解决方案”都说要使用 Stackpanel 或其他东西并设置 DataContext,但它也不起作用。

回答by Ehsan Sajjad

You first have to set the DataContextproperty to the instance of your ViewModel which in this case is SomeThingclass, so you need to set the context in the CodeBehind of the xamlform like:

您首先必须将DataContext属性设置为ViewModel 的实例,在这种情况下是SomeThing类,因此您需要在xaml表单的 CodeBehind 中设置上下文,例如:

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
        Something st = new Something();
        this.DataContext = st;
    }

}

and then in your view i.e xaml file you can set the binding of the control that which particular property to bind for textbox like:

然后在您的视图 ie xaml 文件中,您可以设置控件的绑定,该控件的绑定为文本框绑定的特定属性,例如:

<TextBox x:Name="textbox" Text="{Binding SomeText}">

and you should be using Commandsinstead of using the Click event of Buttons, if you want proper MVVM pattern in your WPF application.

如果您想在 WPF 应用程序中使用正确的 MVVM 模式,您应该使用命令而不是使用按钮的 Click 事件。

You might want to look at How to Implement MVVM in WPF

你可能想看看如何在 WPF 中实现 MVVM

回答by Joe

How were you setting the data context?

你是如何设置数据上下文的?

The data context is where WPF looks for it's bindings. It seems you are trying to bind to your code-behind, in which case you need to set your DataContext to the object itself, and change the field st to a property.

数据上下文是 WPF 查找其绑定的地方。您似乎正在尝试绑定到您的代码隐藏,在这种情况下,您需要将 DataContext 设置为对象本身,并将字段 st 更改为属性。

private Something _st;
public Something st
{
    get
    {
        return _st;
    }
    set
    {
        _st = value;
        OnNotifyPropertyChanged("st");
    }
}

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
    st = new Something();
}

However, if you're always wanting to bind to things inside st, it might make more sense to set the data context to the Something directly:

但是,如果您总是想绑定到 st 内部的东西,那么直接将数据上下文设置为 Something 可能更有意义:

public MainWindow()
{
    InitializeComponent();
    st = new Something();
    this.DataContext = st ;
}

and simply bind like this:

并像这样简单地绑定:

<TextBox Text="{Binding SomeText}">

This means you don't need to implement INotifyPropertyChanged, or have st as a public property on your Window (But you should on Something, firing a change notification in the setter of SomeText).

这意味着您不需要实现 INotifyPropertyChanged,或将 st 作为您的 Window 上的公共属性(但您应该在 SomeText 的 setter 中触发更改通知)。

回答by Sachini Witharana

Since you are using a button click on this scenario I believe a much simpler method would be for you to bind the text in the text box in the button_click method itself: cs file: public partial class MainWindow : Window {

由于您在这种情况下使用按钮单击,我相信一个更简单的方法是让您在 button_click 方法本身的文本框中绑定文本:cs file: public partial class MainWindow : Window {

    public MainWindow()
    {
        InitializeComponent();

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string myText;
        myText = txtBox.Text;
        MessageBox.Show(myText);
    }
}

XAML:

XAML:

<TextBox Name="txtBox">

SO what happens here is Im getting the txt that i have typed on the textBox through the Name of the text box. And in the Button_Click method Im binding the text in the text box to a string and displaying the text through the Message.Show dialog box. Thanks Hope it helps helps. Cheers

所以这里发生的事情是我通过文本框的名称获取我在文本框上输入的 txt。而在 Button_Click 方法中,我将文本框中的文本绑定到一个字符串,并通过 Message.Show 对话框显示该文本。谢谢希望它有帮助。干杯