我使用Prism for Windows Runtime来将View中的事件与ViewModels中的DelegateCommands连接起来。我想知道什么是从包含按钮(或自定义控件派生的freom Button类)的ListView调用命令(例如,选择项目)的最佳方式。我想保留Button控件提供的效果(例如背景更改,倾斜效果)。但按钮遗憾的是吸收了点击事件,其中,在后果,我不能在ListView使用挂钩,例如与下面的XAML(和行为SDK)我的命令:在MVVM中调用ListView中的命令
<ListView ItemsSource="{Binding AvailableItemsList}" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<customControls:NavMenuButton Style="{StaticResource SelectionListMenuButton}" Content="{Binding Nickname}" DescriptionText="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction Command="{Binding ItemSelectedCommand}" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</ListView>
什么将是最好的方式做到这一点?我发现了类似的问题,但这里的区别在于列表项中的控件显然是“窃取”了点击事件(尽管它适用于例如简单的TextBlock)。
为什么不绑定的SelectedItem而不是使用eventrtrigger的东西? – blindmeis 2014-10-09 06:05:57
嗡嗡声你想在你的视图模式中从你的按钮在你的视图模式中调用methode?如果是这样,你只需要使用listviewdatacontext并像这样调用你的命令。 Command =“{Binding DataContext.YourCommand,elementName = yourListView} – MatDev8 2014-10-09 09:22:14
@blindmeis:你的意思是SelectionChanged?我的理解是它没有实现ICommand,所以我需要在代码后面添加一些逻辑。真的问题,但以某种方式打破了模式,我误解了吗? – jerry 2014-10-09 12:23:45