【发布时间】:2009-11-06 11:41:17
【问题描述】:
我是 WPF 概念的新手。我只想在文本框中显示一个字符串。我尝试使用以下 C# 代码和 XAML 将字符串绑定到 TextBox.Text 属性。
C#代码:
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>
在执行时,我的文本框中总是出现空白文本(即使我调用了点击事件)。
我浏览了 stackoverflow 网站,但无法解决问题(尽管很多问题都与这个问题很接近)
如果有任何遗漏或遗漏,有人可以建议我吗?
【问题讨论】:
标签: c# wpf data-binding xaml