使用ViewModel中的多态性绑定到XAML中带有参数的方法 [英] Binding to a method with parameter in XAML using polymorphism in ViewModel

查看:91
本文介绍了使用ViewModel中的多态性绑定到XAML中带有参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ResultView中有一个带有六个选项卡的TabControl.位于该视图后面的ViewModel可以是ResultTypeOneViewModel或ResultTypeTwoViewModel,它们每个都派生自ResultViewModel,但是您可以将结果查看器与任何一种结果类型互换使用.

I have a TabControl with six tabs in my ResultView. The ViewModel that sits behind this View can be either a ResultTypeOneViewModel or ResultTypeTwoViewModel, each of which derives from ResultViewModel but you can interchangeably use the result viewer with either result type.

区别在于,在ResultTypeOneViewModel中,标签1& 3需要可见,其余隐藏. 在ResultTypeTwoViewModel中,选项卡2、3、4、5、6必须是可见的,而选项卡1是隐藏的.

The difference is that in ResultTypeOneViewModel, tabs 1 & 3 need to be visible and the rest hidden. In ResultTypeTwoViewModel, tabs 2, 3, 4, 5, 6 need to be visible and tab 1 hidden.

我想通过

<TabItem Name="1" Visibility={Binding IsTabVisible(0)}>
<TabItem Name="2" Visibility={Binding IsTabVisible(1)}>
<TabItem Name="3" Visibility={Binding IsTabVisible(2)}>
etc...

在ResultsViewModel中有一个抽象方法声明,例如

And have an abstract method declaration in ResultsViewModel such as

public abstract Visibility IsTabVisible(int index);

在ResultsOneViewModel中有

And in ResultsOneViewModel have

public override Visibility IsTabVisible(int index)
{
    if (index == 0 || index == 2) return Visibility.Visible;
    return Visibility.Hidden;
}

在ResultsTwoViewModel中有

And in ResultsTwoViewModel have

public override Visibility IsTabVisible(int index)
{
    if (index == 0) return Visibility.Hidden;
    return Visibility.Visible;
}

但是我无法弄清楚如何通过iN WPF XAML绑定通过参数调用类似的方法.

But I cannot figure out how to call a method like this with a parameter through bindings iN WPF XAML.

任何人都可以建议我该怎么做,或者如果无法通过这种方法来解决我的另一种方法呢?

Can anyone suggest how I can do this or if it's not possible via this method, another way I could solve this problem?

推荐答案

要直接回答您的问题,您可以使用ObjectDataProvider来为您调用方法,以便处理结果:

To answer your question directly, you can use the ObjectDataProvider to call a method for you so you can work with the results:

xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:Windows="clr-namespace:System.Windows;assembly=PresentationCore"

...

<Window.Resources>
    <ObjectDataProvider x:Key="IsTab1VisibleMethod" 
        ObjectType="{x:Type Windows:Visibility}" IsAsynchronous="True" 
        MethodName="IsTabVisible">
        <ObjectDataProvider.MethodParameters>
            <System:Int32>0</System:Int32>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

然后,您应该能够像这样访问结果(但每个TabItem都需要其中之一):

You should then be able to access the result like this (but you'd need one of these for each TabItem):

<TabItem Visibility="{Binding Source={StaticResource IsTab1VisibleMethod}}" />

但是,通常不是在代码中操作UI属性(如Visibility)的好习惯,因此更好的主意是使用BooleanToVisibilityConverterBindVisibility属性到bool的值,例如@ Highcore建议.您可以在将按钮可见性绑定到ViewModel中的bool值在此处发布在StackOverflow上.

However, it's generally not good practice to manipulate UI properties such as Visibility in code, so a better idea would be to use a BooleanToVisibilityConverter to Bind the Visibility properties to bool values as @Highcore suggested. You can see an example of this in the Binding a Button Visibility to bool value in ViewModel post here on StackOverflow.

我认为,更好的解决方案是为每个视图模型简单地提供一个视图.

In my opinion, an even better solution would be to simply provide one view for every view model.

这篇关于使用ViewModel中的多态性绑定到XAML中带有参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆