I have a list view and 2 resources for display the list's view: BooksGridView & ImageDetailView.
The ViewModel has a string property named ViewMode, which contains the name of the view i currently want to display. (It is changed from another control, using toolbars)
I am trying to change the selected view by using DataTrigger
, but I cant seem to get the View property to change.
When i set the View resource directly, the correct view is displayed. I also added background changes to make sure the data trigger is activated, and the background did change.
So I'm obviously missing something here...
<UserControl x:Class="eLibrary.View.FilteredBooksView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Converters="clr-namespace:eLibrary.Converters"
xmlns:Controls="clr-namespace:eLibrary.Controls"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:local="clr-namespace:eLibrary"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Converters:CoverImageConverter x:Key="CoverImageConverter"/>
<BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
<GridView x:Key="BooksGridView">
...
</GridView>
<Controls:TileView x:Key="ImageDetailView">
...
</Controls:TileView>
<CollectionViewSource x:Key="sortedBooks" Source="{Binding Books}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Title" Direction="Ascending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<Style TargetType="{x:Type ListView}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ViewMode}" Value="BooksGridView">
<Setter Property="View" Value="{StaticResource BooksGridView}"/>
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=ViewMode}" Value="ImageDetailView">
<Setter Property="View" Value="{StaticResource ImageDetailView}" />
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<ListView
VerticalAlignment="Stretch"
Name="BooksListView"
View="{StaticResource BooksGridView}"
SelectionMode="Extended"
ItemsSource="{Binding Source={StaticResource sortedBooks}}">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
<Setter Property="Visibility" Value="{Binding Path=ShouldDisplay, Converter={StaticResource BoolToVisConverter} }" />
</Style>
</ListView.Resources>
</ListView>
</UserControl>
Thanks
See Question&Answers more detail:os