2017-04-26 35 views
1

我有一個自定義設計的窗口如下所示。如何重用WPF中的自定義設計窗口?

Custom Window

以下是我的XAML設計,省略了簡單的樣式。

<Window x:Class="CustomWindowBase.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:CustomWindowBase" 
      mc:Ignorable="d" 
      Title="CustomWindow" Height="600" Width="870" WindowStartupLocation="CenterScreen" 
      ResizeMode="NoResize" AllowsTransparency="True" WindowStyle="None" Background="Transparent"> 
    <Border Style="{StaticResource MainWindowBorderStyle}"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="35"/> 
       <RowDefinition Height="590*"/> 
      </Grid.RowDefinitions> 

      <Border Grid.Row="0" Style="{StaticResource TitleBarBorderStyle}"> 
       <Grid> 
        <TextBlock Style="{StaticResource TitleStyle}" Text="Custom Window"/> 
        <Button x:Name="BtnClose" Style="{StaticResource CloseButtonStyle}"/> 
       </Grid> 
      </Border> 

      <Grid Grid.Row="1"> 
       <!-- Different User Control Here --> 
      </Grid> 
     </Grid> 
    </Border> 
</Window> 

該窗口後面的代碼有兩個事件支持關閉/拖動操作。

我該如何重複使用這個shell到我的應用程序可能打開的每個其他窗口,有點像可以被繼承的基類?

如果可能的話,我不想在後面的代碼中做太多事情,例如實例化此窗口外殼的實例並將其內容與另一個用戶控件分配。

非常感謝您的幫助。

回答

2

叫它把XAML的東西在ControlTemplate

對於<!-- Different User Control Here -->部分,請插入<ContentPresenter />。它知道該怎麼做。 它只是知道。

將該模板和其他所需的屬性值與Style一起應用。

的App.xaml

<ControlTemplate x:Key="MyWindowTemplate" TargetType="Window"> 
    <Border Style="{StaticResource MainWindowBorderStyle}"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="35"/> 
       <RowDefinition Height="590*"/> 
      </Grid.RowDefinitions> 

      <Border Grid.Row="0" Style="{StaticResource TitleBarBorderStyle}"> 
       <Grid> 
        <TextBlock Style="{StaticResource TitleStyle}" Text="Custom Window"/> 
        <Button x:Name="BtnClose" Style="{StaticResource CloseButtonStyle}"/> 
       </Grid> 
      </Border> 

      <Grid Grid.Row="1"> 
       <ContentPresenter 
        /> 
      </Grid> 
     </Grid> 
    </Border> 
</ControlTemplate> 

<Style TargetType="Window" x:Key="MyWindowStyle"> 
    <Setter Property="Template" Value="{StaticResource MyWindowTemplate}" /> 
    <Setter Property="Height" Value="600" /> 
    <Setter Property="Width" Value="870" /> 
    <Setter Property="ResizeMode" Value="NoResize" /> 
    <Setter Property="AllowsTransparency" Value="True" /> 
    <Setter Property="WindowStyle" Value="None" /> 
    <Setter Property="Background" Value="Transparent" /> 
</Style> 

用法:

<Window x:Class="CustomWindowBase.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:CustomWindowBase" 
    mc:Ignorable="d" 
    Style="{StaticResource MyWindowStyle}" 
    WindowStartupLocation="CenterScreen" 
    > 
    <!-- This content will be displayed in the ContentPresenter. --> 
    <StackPanel Orientation="Vertical"> 
     <Label>Blah blah</Label> 
     <ComboBox ItemsSource="{Binding SomeVMThing}" /> 
    </StackPanel> 
</Window> 

可悲的是,你不能設置WindowStartupLocation的風格,因爲它不是一個DependencyProperty。

您提到了一些關閉按鈕事件。我不確定你現在如何設置這些,但會有辦法。

+1

哇,那內容主持人是一些魔術!這正是我所尋找的,謝謝你的幫助,標記爲答案。 對於這些事件,我在我的App.xaml.cs中實現了Close和Drag,通過將發送者轉換爲其控件並執行Window.GetWindow()實例並調用Close和DragMove方法。 – IvanJazz

0

如果我說得對,那就沒問題。從窗外用

new MainWindow().ShowDialog();

+0

不,對不起,我的意思是我可能創建和使用的所有其他窗口具有相同的外觀和感覺。不打開他們.. – IvanJazz