Based off the sample on MSDN, the following works at changing the view based on a change in the ViewModel. The only difference I can see with your code is the use of DynamicResource
:
<Window x:Class="SDKSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Custom View"
xmlns:l="clr-namespace:SDKSample"
Width="400" Height="500"
SourceInitialized="Window_SourceInitialized">
<Window.Resources>
<DataTemplate x:Key="centralTile">
<StackPanel Height="100" Width="90">
<Grid Width="70" Height="70" HorizontalAlignment="Center">
<Image Source="{Binding XPath=@Image}" Margin="6,6,6,9"/>
</Grid>
<TextBlock Text="{Binding XPath=@Name}" FontSize="13"
HorizontalAlignment="Center" Margin="0,0,0,1" />
<TextBlock Text="{Binding XPath=@Type}" FontSize="9"
HorizontalAlignment="Center" Margin="0,0,0,1" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="iconTemplate">
<DockPanel Height="33" Width="150">
<Image Source="{Binding XPath=@Image}" Margin="2"/>
<TextBlock DockPanel.Dock="Top" Text="{Binding XPath=@Name}"
FontSize="13" HorizontalAlignment="Left"
Margin="0,0,0,1" />
<TextBlock Text="{Binding XPath=@Type}" FontSize="9"
HorizontalAlignment="Left" Margin="0,0,0,1" />
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="checkbox">
<CheckBox IsChecked="{Binding IsSelected, RelativeSource= {RelativeSource AncestorType=ListViewItem}}"
Margin="0,1,1,1" >
</CheckBox>
</DataTemplate>
<XmlDataProvider x:Key="myXmlDataBase" XPath="/myXmlData">
<x:XData>
<myXmlData xmlns="">
<Item Name = "Fish" Type="fish" Image="imagesfish.png"/>
<Item Name = "Dog" Type="animal" Image="imagesdog.png"/>
<Item Name = "Flower" Type="plant" Image="imagesflower.jpg"/>
<Item Name = "Cat" Type="animal" Image="imagescat.png"/>
</myXmlData>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="DisplayImage">
<StackPanel Width="50">
<Image Source="{Binding XPath=@Image}"/>
</StackPanel>
</DataTemplate>
<GridView x:Key="gridView">
<GridViewColumn CellTemplate="{StaticResource checkbox}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=@Name}"/>
<GridViewColumn Header="Type" DisplayMemberBinding="{Binding XPath=@Type}"/>
<GridViewColumn Header="Image" CellTemplate="{StaticResource DisplayImage}"/>
</GridView>
<l:PlainView x:Key="tileView" ItemTemplate="{StaticResource centralTile}" ItemWidth="100"/>
<l:PlainView x:Key="iconView" ItemTemplate="{StaticResource iconTemplate}" ItemWidth="150"/>
<Style TargetType="{x:Type ListView}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ViewName}" Value="iconView">
<Setter Property="View" Value="{DynamicResource iconView}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=ViewName}" Value="tileView">
<Setter Property="View" Value="{DynamicResource tileView}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=ViewName}" Value="gridView">
<Setter Property="View" Value="{DynamicResource gridView}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<ListView Name="lv"
ItemsSource="{Binding Source={StaticResource myXmlDataBase}, XPath=Item}"
FontSize="12"
Background="LightBlue" >
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="gridView" Click="SwitchViewMenu"/>
<MenuItem Header="iconView" Click="SwitchViewMenu"/>
<MenuItem Header="tileView" Click="SwitchViewMenu"/>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
<TextBlock FontSize="16" Foreground="Blue">
CurrentView: <TextBlock Name="currentView" Text="{Binding Path=ViewName}"/>
</TextBlock>
<TextBlock>
Right-click in the content window to change the view.
</TextBlock>
</StackPanel>
</Window>
Code behind file:
using System;
using System.Windows;
using System.Windows.Controls;
namespace SDKSample
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public MainViewModel ViewModel
{
get { return this.DataContext as MainViewModel; }
}
void SwitchViewMenu(object sender, RoutedEventArgs args)
{
MenuItem mi = (MenuItem)sender;
ViewModel.ViewName = mi.Header.ToString();
}
private void Window_SourceInitialized(object sender, EventArgs e)
{
ViewModel.ViewName = "gridView";
}
}
}
And finally the ViewModel class:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace SDKSample
{
public class MainViewModel : INotifyPropertyChanged
{
public string ViewName
{
get { return viewName; }
set
{
if (viewName == value)
return;
viewName = value;
NotifyPropertyChanged("ViewName");
}
}
private string viewName;
public event PropertyChangedEventHandler PropertyChanged;
void NotifyPropertyChanged(string name)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…