gpt4 book ai didi

c# - 从MVVM中的ListView调用命令

转载 作者:行者123 更新时间:2023-12-03 10:28:13 27 4
gpt4 key购买 nike

我正在使用Prism for Windows Runtime通过ViewModels中的DelegateCommands来连接Views中的事件。我想知道从包含Buttons(或从Button类派生的自定义控件)的ListView中调用命令(例如选择项目)的最佳方法是什么。我想保留Button控件提供的效果(例如背景变化,倾斜效果)。但是不幸的是,按钮吸收了click事件,因此,我无法在ListView中使用它来挂接命令,例如,使用以下XAML(和Behaviors SDK):

<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>

实现这一目标的最佳方法是什么?我发现了类似的问题,但是这里的区别在于列表项中的控件显然是在“窃取” click事件(尽管它可以与例如简单的TextBlock一起正常使用)。

最佳答案

为了解决这个问题,这是基于上面MatDev8的评论的解决方案(谢谢!):

<ListView x:Name="myListView" ItemsSource="{Binding AvailableItemsList}" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<customControls:CustomTextButton Style="{StaticResource SelectionListMenuButton}"
Content="{Binding Nickname}"
DescriptionText="{Binding Name}"
Command="{Binding DataContext.ItemSelectedCommand, ElementName=myListView}"
CommandParameter="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

关于c# - 从MVVM中的ListView调用命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26267414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com