我一直在使用 MVVM Light 开发一个示例项目,我想知道如何绑定(bind) TextBox 文本值并将其在 View 和 View 模型之间传递。这是我第一次使用 MVVM Light,所以我对此很陌生。
基本上,用户将在文本框名称中输入项目名称,然后单击“新建项目”按钮,该按钮应生成一个以项目名称文本框中输入的内容命名的数据库。
查看:
<UserControl x:Class="Sample.Views.NavigationTree.NewProjectView"
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:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
DataContext="{Binding NewProjectView, Source={StaticResource Locator}}">
<Grid>
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<mui:BBCodeBlock BBCode="Project Name"/>
<Label Width="10"/>
<TextBox Text="{Binding ProjName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Width="120"/>
</StackPanel>
<Label Height="10"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Label Width="85"/>
<Button Content="New Project" Margin="0,0,3,0" Command="{Binding AddProjectCommand}" IsEnabled="{Binding IsUserAdmin}" Grid.Column="2" Grid.Row="0"/>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
View 模型:
using Sample.Model.Database;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Text;
namespace Sample.ViewModel
{
/// <summary>
/// This class contains properties that a View can data bind to.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class NewProjectViewModel : ViewModelBase
{
private string _projName;
//Binding AddProjectCommand
public RelayCommand AddProjectCommand { get; set; }
private string consoleText { get; set; }
private StringBuilder consoleBuilder = new StringBuilder(360);
/// <summary>
/// Initializes a new instance of the NewProjectViewModel class.
/// </summary>
public NewProjectViewModel()
{
this.AddProjectCommand = new RelayCommand(() => AddProject());
}
public void AddProject()
{
ProjectDbInteraction.CreateProjectDb(_projName);
}
public string ProjName
{
get { return _projName; }
set
{
if (value != _projName)
{
_projName = value;
RaisePropertyChanged("ProjName");
}
}
}
public string ConsoleText
{
get { return consoleText; }
set
{
consoleBuilder.Append(value);
consoleText = consoleBuilder.ToString();
RaisePropertyChanged("ConsoleText");
}
}
}
}
那么如何将 ProjName 绑定(bind)传递到 View 和 View MOdel 之间呢?
最佳答案
看起来不错,您只需要在 View 和 ViewModel 之间创建关联即可。基本上,将 View 的 DataContext 设置为 ViewModel。
您可以通过几种方法来做到这一点,我将展示两种: 1) 在 View 的代码隐藏中,您可以创建 View 模型的实例 (ViewModel vm=new ViewModel()),然后使用 this.DataContext=vm; 对其进行分配; 2) 您可以使用 XAML 数据模板。像这样,Home 是一个 View ,HomeVM 是 View 模型。
在
<Window
.
.
.
xmlns:HomeView="clr-namespace:Bill.Views"
xmlns:HomeVM="clr-namespace:Bill.ViewModels"
>
<Window.Resources>
<!--Home User Control and View Model-->
<DataTemplate DataType="{x:Type HomeVM:HomeVM}">
<HomeView:Home/>
</DataTemplate>
</Window.Resources>
第一个似乎更适合我的正常需求......
关于c# - 如何将 TextBox 中的值绑定(bind)到 Mvvm Light 中的 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26768985/