数据从代码到XAML的简单绑定 [英] Simple Binding of Data from code behind to XAML

查看:119
本文介绍了数据从代码到XAML的简单绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是WPF概念的新手。我想在文本框中显示一个字符串。我尝试了以下C#代码和XAML将一个字符串绑定到一个TextBox.Text属性。
C#代码:


I am new to WPF concepts. I want to just display a string in a textbox. I tried the following C# code and XAML to bind a string to a TextBox.Text property. C# code:

public partial class Window1 : Window
{
    public int TmpVal;
    public string TmpStr;

     public Window1()
    {
        TmpVal = 50;
        TmpStr = "Windows Created";
        InitializeComponent();
        this.DataContext = this;
    }
    private void viewButton_Click(object sender, RoutedEventArgs args)
    {
        TmpStr = "Button clicked";
    }
}

}

XAML:

 <Window x:Class="TestWPF.Window1"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="800" x:Name="ThisWindow">
<Grid>
    <TextBox Name="txtTest1" Margin="200,0,200,200" HorizontalAlignment="Left" Height="50" Width="200" Text="{Binding TmpStr, ElementName=ThisWindow}" />
    <Button Name="butTest1"  Click="viewButton_Click">Test123</Button>
</Grid>
</Window>

执行时,我总是在我的文本框中得到空白文本(即使我调用click事件)。

我浏览了stackoverflow网站,但是无法解决问题(尽管很多问题都接近这个问题)
可以有人建议我,如果有什么被忽视或错过了?

On execution I always get blank text in my textbox (even when I invoke the click event).
I browsed through the stackoverflow site but couldn't solve the problem (though many questions were close to this one)
Can someone suggest me if anything is overlooked or missed out?

推荐答案

数据绑定不适用于字段。使用属性代替:

Databinding does not work with fields. Use Properties instead:

public int TmpVal {get; set;}
public string TmpStr {get; set;}

此外,如果您希望文本框自动从您的数据中提取更改,您最好需要实施INotifyPropertyChanged或使其成为依赖属性或每个XXX属性此功能不再工作)的XXXChanged事件。

Also if you want the textbox to automatically pick up changes from your data you would ideally need to implement INotifyPropertyChanged or make it a dependency property or have a XXXChanged event for each XXX property (this doesn't work anymore).

<Window x:Class="WpfApplication5.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" x:Name="ThisWindow">
    <StackPanel>
        <TextBox Name="txtTest1" Text="{Binding TmpStr, ElementName=ThisWindow}" />
        <Button Name="butTest1"  Click="viewButton_Click">Test123</Button>
    </StackPanel>
</Window>

后面的代码:

public partial class Window1 : Window, INotifyPropertyChanged
{
    public Window1()
    {
        this.TmpStr = "Windows Created";
        this.InitializeComponent();
        this.DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public string TmpStr { get; set; }

    public int TmpVal { get; set; }

    private void viewButton_Click(object sender, RoutedEventArgs args)
    {
        this.TmpStr = "Button clicked";
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs("TmpStr"));
        }
    }
}

这篇关于数据从代码到XAML的简单绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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