【发布时间】:2016-12-28 00:56:25
【问题描述】:
很抱歉,这个问题非常基础。我刚刚学习了 WPF,但未能将简单的两种方式绑定到 textbox.text 到字符串属性。
XAML 代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="StuInfo">
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="10,26,0,0" TextWrapping="Wrap" Text="{Binding Path=str,Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>
<Button x:Name="button" Content="Check" HorizontalAlignment="Left" Margin="10,67,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
C#代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
str = "OK";
}
public string str { get; set; }
private void button_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine(str);
}
}
首先,文本框没有显示“OK”,而是空白。然后,我在文本框中输入了不同的文本,例如:“blablabla”,不带引号。然后我单击按钮检查我的 str 属性是否已更新。显然,str 仍然包含“OK”。
我在这里做错了什么?使绑定工作我错过了什么?
【问题讨论】:
标签: c# wpf xaml binding textbox