我想學習WPF和我堅持最初的綁定示例。 我想從代碼中的屬性中看到文本框中的文本。我已經看到一些相關的問題,我已經嘗試了他們的答案,但不幸的是他們都不符合我的需要。此外,如果有人可以告訴我一些很好的教程,從基礎開始並進入高級水平。我正在看MSDN教程,但我發現它們非常複雜(或太多的文本)使事情變得更加複雜。數據綁定沒有錯誤,但不能看到綁定的輸出
MainWindow.xaml.cs
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public string Name { get; set; }
public MainWindow()
{
Name = "Dummy";
InitializeComponent();
DataContext = this;
}
}
}
MainWindow.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>
<TextBox Text="{Binding Path=Name}"></TextBox>
</Grid>
</Window>
下一步是使用視圖模型類,如其他答案所示。 – Clemens
感謝它的作品!我會接受它 – Smallestearth