Xaml 数据绑定和EventToCommand
我正试图使用EventToCommand初始化我的ViewModel,但命令没有启动。我怀疑这是因为触发器部分不在数据绑定容器中,但在我的示例中如何做到这一点?如果可能的话,我会坚持使用纯XAMLXaml 数据绑定和EventToCommand,xaml,mvvm-light,Xaml,Mvvm Light,我正试图使用EventToCommand初始化我的ViewModel,但命令没有启动。我怀疑这是因为触发器部分不在数据绑定容器中,但在我的示例中如何做到这一点?如果可能的话,我会坚持使用纯XAML <Window x:Class="MVVMSample.Home" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsof
<Window x:Class="MVVMSample.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:MVVMSample.ViewModels"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
d:DataContext="{d:DesignInstance Type=viewModels:HomeViewModel, IsDesignTimeCreatable=True}"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewModels:HomeViewModel x:Key="ViewModel" x:Name="ViewModel" />
</Window.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding LoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid DataContext="{StaticResource ViewModel}">
<TextBlock Text="{Binding PersonCount}" />
</Grid>
</Window>
你是对的,datacontext是问题的一部分,但我会按照设计使用mvvm light来解决它 如果您使用的是MVVM_灯光,则应使用视图模型定位器。它是框架的主要支柱。我使用mvvm light来了解mvvm原理。我非常喜欢它,因为它很简单,可以让我以尽可能快的速度学习
<Window x:Class="MVVMSample.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:MVVMSample.ViewModels"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
d:DataContext="{d:DesignInstance Type=viewModels:HomeViewModel, IsDesignTimeCreatable=True}"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewModels:HomeViewModel x:Key="ViewModel" x:Name="ViewModel" />
</Window.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding LoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid DataContext="{StaticResource ViewModel}">
<TextBlock Text="{Binding PersonCount}" />
</Grid>
</Window>
在mvvm灯光下,在app.xaml中声明viewmodellocator
<Application.Resources>
<ResourceDictionary>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
</ResourceDictionary>
</Application.Resources>
希望这有帮助为什么要使用此方法初始化viewmodel?如果您使用的是mvvm灯光,则viewmodel定位器会为您执行此操作。您使用viewmodel作为datacontext通过viewmodellocator进行绑定。我不打算使用viewmodellocator。我应该吗?