0
votes

In my WPF application I'm trying to navigate to other 'pages' using the ContentControl. I have this working so far, in my MainViewModel I have initiated the other viewmodels that should be a part of the MainViewModel.

I display my views with a datatemplate like this:

        <DataTemplate DataType="{x:Type vm:NewsViewModel}">
            <Views:NewsView />
        </DataTemplate>

I got a ItemsControl with TextBlocks to display the View(models) PageName property, when I click on this, it does set the 'CurrentView' property to the according ViewModel and it gets displayed. So this aint the problem... However, the issue I come across now is how to let the textblock display the CurrentView I have, for example I want it to be another color then the rest of the textblocks so the user can see which view(model) is active.

I tried to do this in the Style for the textblock with a DataTrigger but this only accepts constant values, any ideas?

2

2 Answers

1
votes

Why not switch the ItemsControl to a ListBox since it has built-in selection features? You can style it so it hides the selection highlight and looks the same as your ItemsControl, and base your trigger off of ListBoxItem.IsSelected.

If you don't want to do that, you can probably use a IMultiValueConverter to pass the current ViewModel and the active ViewModel to a converter, which would return True if the items are the same, or false if not.

0
votes

Create a IValueConverter that returns if the supplied view is the active view and add it to the binding of the DataTrigger.

Sample Converter:

public class IsViewActiveConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value == [activeView];
    }
}

Sample xaml:

<UserControl.Resources>
     <local:IsViewActiveConverter x:Key="IsViewActive"/>
</UserControl.Resources>

<DataTrigger Binding="{Binding View, Converter={StaticResource IsViewActive}}" Value="True">