Binding to a method with parameter in XAML using polymorphism in ViewModel

后端 未结 3 1878
花落未央
花落未央 2021-02-20 09:34

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 deri

3条回答
  •  情歌与酒
    2021-02-20 10:00

    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"
    
    ...
    
    
        
            
                0
            
        
    
    

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

    
    

    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.

提交回复
热议问题