也許你應該嘗試使用ContentTemplateSelector。這裏有一個很好的example ..
下面是一個簡單的例子,我做了適合您的場景。我有一個有邊框的窗口,邊框內有一個ContentControl,它有一個模板選擇器,可以讓你選擇要顯示的視圖。
這裏的觀點:
看看當地:MyContentTemplateSelector標籤。
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="FirstTemplate">
<TextBlock Text="First" />
</DataTemplate>
<DataTemplate x:Key="SecondTemplate">
<TextBlock Text="Second" />
</DataTemplate>
<local:MyContentTemplateSelector FirstTemplate="{StaticResource FirstTemplate}" SecondTemplate="{StaticResource SecondTemplate}"
x:Key="mytemplateSelector" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border BorderThickness="1" BorderBrush="Red" Grid.Row="0">
<ContentControl ContentTemplateSelector="{StaticResource mytemplateSelector}" Content="{Binding SelectedViewModel}"/>
</Border>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1">
<Button Command="{Binding SelectFirstViewModel}">Go to First Template</Button>
<Button Command="{Binding SelectSecondViewModel}">Go to Second Template</Button>
</StackPanel>
</Grid>
</Window>
這裏的視圖模型:
public class MainVm : ViewModelBase
{
private FirstVm _FirstViewModel;
public FirstVm FirstViewModel
{
get { return _FirstViewModel; }
set { Set(ref _FirstViewModel, value); }
}
private SecondVm _SecondViewModel;
public SecondVm SecondViewModel
{
get { return _SecondViewModel; }
set { Set(ref _SecondViewModel, value); }
}
private ViewModelBase _SelectedViewModel;
public ViewModelBase SelectedViewModel
{
get { return _SelectedViewModel; }
set { Set(ref _SelectedViewModel, value); }
}
public ICommand SelectFirstViewModel
{
get
{
return new RelayCommand(() => { this.SelectedViewModel = FirstViewModel; });
}
}
public ICommand SelectSecondViewModel
{
get
{
return new RelayCommand(() => { this.SelectedViewModel = SecondViewModel; });
}
}
public MainVm()
{
FirstViewModel = new FirstVm();
SecondViewModel = new SecondVm();
SelectedViewModel = this.FirstViewModel;
}
}
這些可能是你有你的網頁的任何視圖模型:
public class FirstVm : ViewModelBase
{
}
public class SecondVm : ViewModelBase
{
}
而這裏的模板選擇。這是重要的部分。無論何時您更改ContenControl的內容,在這種情況下,內容都會綁定到MainVm的SelectedViewmodel屬性,此類中的SelectTemplate方法將被調用。這就是你放置視圖或數據模板顯示的邏輯的地方。
public class MyContentTemplateSelector : DataTemplateSelector
{
public DataTemplate FirstTemplate { get; set; }
public DataTemplate SecondTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is FirstVm)
return FirstTemplate;
if (item is SecondVm)
return SecondTemplate;
return null;
}
}
它看起來象是這樣的:
我想我可以設計定製的窗口樣式和隱藏的按鈕來達到我想要的這裏提到 HTTP:/ /www.kirupa.com/blend_wpf/custom_wpf_windows.htm –
在這些屏幕截圖中,我沒有看到任何自定義形狀的窗口。你仍然可以通過最常見的min/max/close按鈕在頂部看到win7 aero窗口....但是你可以通過編輯樣式模板找到正確的方式來定製你想要的東西。 –
您是否正在尋找某種只有一個窗口並且可以更改內容的母版頁? – Lance