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

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

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

I wanted to do it through something like

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

And have an abstract method declaration in ResultsViewModel, for example

 public abstract Visibility IsTabVisible(int index); 

And in ResultsOneViewModel there is

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

And in ResultsTwoViewModel there is

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

But I can’t figure out how to call a method like this with a parameter via iN WPF XAML bindings.

Can someone suggest how I can do this, or if this is not possible with this method, in another way I could solve this problem?

+8
wpf mvvm binding
source share
2 answers

To answer your question directly, you can use ObjectDataProvider to call the method for you so that 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> 

Then you can access this result (but you will need one of them for each TabItem ):

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

However, as a rule, it is not a good practice to manipulate UI properties such as Visibility in your code, so it would be better to use the BooleanToVisibilityConverter to Bind Visibility properties for bool values ​​like @ Greetings. You can see an example of this in the binding of the Visibility button to the bool value in the ViewModel here in StackOverflow.

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

+9
source share

The best suggestion for this question is to use the TabItem style .DataTrigger style as follows:

 <TabItem> <TabItem.Style> <Style target="{x:Type TabItem}"> <Style.DataTriggers> <DataTrigger Binding="{Binding IsTabVisible}" Value="False"> <Setter Property="Visibility" Value = "Collapsed"/> </DataTrigger> </Style.DataTrigers> </Style> <TabItem.Style> </TabItem> 
+2
source share

All Articles