我正在学习MVVM和WPF。我的项目中有一个xaml
文件,后面的代码中有一个简单的单击事件处理程序。
现在我想在MVVM中做同样的事情。我读了很多文章,也读了很多sof的答案。但仍然无法做到这一点。
谁能举一个简单的例子,在MVVM中完成按钮单击事件。
编辑
<Window x:Class="WhiteBalance.BaseCalWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:NumberUpDownControl;assembly=NumberUpDownControl"
xmlns:viewn="clr-namespace:WhiteBalance.ViewModels"
Title="RefImgSettingWindow" Height="900" Width="1000" ResizeMode="NoResize"
BorderThickness="4">
<Window.Resources>
<viewn:DashBoardViewModel x:Key="demokey"></viewn:DashBoardViewModel>
</Window.Resources>
<Grid x:Name="gdParent" DataContext="{StaticResource demokey}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="490" />
<ColumnDefinition Width="488*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="300" />
<RowDefinition Height="300" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0">
<Label Content="{Binding Path=NAME,Mode=TwoWay}" Height="28" Name="lblTest" />
<Button Content="Capture" Height="23" Name="btnCapture" Width="75" Command="{Binding Path=SaveCommand}"
Canvas.Left="94" Canvas.Top="254" />
</StackPanel>
</Grid>
</Window>
namespace WhiteBalance.ViewModels
{
public class DashBoardViewModel: ObservableObject
{
private string _name = "dsqdasd";
public string NAME
{
get { return _name; }
set { _name = value; }
}
public ICommand SaveCommand
{
get;
set;
}
private bool CanExecuteSaveCommand()
{
return true; // !string.IsNullOrEmpty(LastName);
}
private void CreateSaveCommand()
{
SaveCommand = new RelayCommand(SaveExecute, CanExecuteSaveCommand);
}
public void SaveExecute()
{
//Person.Save(_newPerson);
NAME = "Changed Name";
}
public DashBoardViewModel()
{
//objModel.TestText = "This will change";
NAME = "TestName";
}
}
}
提前致谢。