c# - 有条件地绑定(bind)到 Xamarin XAML MVVM 中的两个不同命令

标签 c# xaml mvvm xamarin.forms

这是我的 ListView 的数据模板

<DataTemplate>
    <StackLayout Orientation="Horizontal">
        <Label Text="{Binding Name}" />
        <StackLayout.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding Path=BindingContext.Command1, Source={x:Reference Name=MainNavigationPage}}" CommandParameter="{Binding Property_A_Integer}"></TapGestureRecognizer>
            <TapGestureRecognizer Command="{Binding Path=BindingContext.Command2, Source={x:Reference Name=MainNavigationPage}}" CommandParameter="{Binding Property_B_Enum}"></TapGestureRecognizer>
        </StackLayout.GestureRecognizers>
    </StackLayout>
</DataTemplate>

Property_B_Enum 的类型为 MyEnum
public enum MyEnum
{
    None,
    Fruit,
    Veggies,
    Meat,
    Cookie
}

在上面的代码中,有两个手势识别器。当 ListView 绑定(bind)到 VM 时,项目通过 ItemsSource 进入。某些项目的 Property_B_Enum 设置为“无”。当这种情况发生时,我希望第一个手势识别器在那里,而不是第二个。反之亦然。

这感觉像是触发器的一个很好的用途,但我不知道如何设置它。

最佳答案

您可以使用数据模板选择器来执行此操作。

有 2 个模板,一个带有 Command1 ,一个带有 Command2 并使用模板选择器根据 Property_B_Enum 显示一个或另一个

More details here

编辑

These are slightly better instructions

或者您可以在命令参数中传递整个项目,并将获取正确属性的逻辑放入命令处理程序中

关于c# - 有条件地绑定(bind)到 Xamarin XAML MVVM 中的两个不同命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47805635/

相关文章:

C# 自动实现的属性帮助

javascript - Knockout JS 入门

wpf - Entity Framework /WPF - 直接绑定(bind)到 DbSet<T>.Local 是否明智?

wpf - 如何使用 WPF Toolkit 将多个图表与多个系列绑定(bind)?

c# - 谁能给我解释一下这个 lambda 表达式?这让我发疯

c# - 模型更改后更新数据库 - Entity Framework 7

c# - Xamarin.Forms ScrollView 增加了额外的空间

c# - WinRT 中 TextBlock 的外发光效果

c# - WPF:绑定(bind) TreeView

c# - 防止 Entity Framework 在数据库中创建外键