我有一個列表視圖和2個資源來顯示列表視圖:BooksGridView & ImageDetailView。WPF:通過DataTrigger設置ListView視圖
ViewModel有一個名爲ViewMode的字符串屬性,它包含我當前想要顯示的視圖的名稱。 (它從另一個控件,使用工具欄更改)
我試圖通過使用DataTrigger
來更改選定的視圖,但我似乎無法使View屬性發生更改。
當我直接設置視圖資源時,顯示正確的視圖。我還添加了背景更改以確保數據觸發器已激活,並且背景確實發生了變化。
所以我缺少明顯的東西在這裏...
<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>
感謝
感謝!,兩個改變使它工作:第一個是DynamicResource,第二個是刪除ListView下的初始View定義。 – Amirshk 2010-11-15 23:48:33
很高興幫助:) – Grammarian 2010-11-16 10:11:47
你可以發佈MSDN示例的href: – Amirshk 2010-11-16 17:52:31