我知道这可能是一个新手问题,但我对 WPF 还是很陌生。现在,我的大多数文本框、标签、文本 block 等...都根据逻辑隐藏在 xaml.cs 代码后面。我想知道是否有办法将我的控件从我的 xaml 文件映射到 View 模型?
<TextBlock Name="txtBlockName" Text="Name:" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="15,0,0,0" Visibility="Hidden" />
<TextBox Name="txtName" Width="auto" HorizontalAlignment="Left" VerticalAlignment="Bottom" Visibility="Hidden" />
最佳答案
确保将 View 模型设置为控件的 DataContext。然后你可以使用类似的东西
<TextBox Text="{Binding PropertyName, Mode=TwoWay}" Name="txtName" Width="auto" HorizontalAlignment="Left" VerticalAlignment="Bottom" />
将 TextBox 的 Text 绑定(bind)到 View 模型的 PropertyName
关于.net - 将控件映射到 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10516268/