2016-10-05 2 views
0

J'essaie de créer un style de fenêtre personnalisé. L'objectif est de créer un modèle qui puisse être utilisé par toutes les fenêtres de mon application. Le modèle contient la barre d'outils, le titre et "la zone qui sera utilisée par la fenêtre". Le problème est le suivant: lorsque j'utilise mon style, je ne peux plus ajouter de grille et de contrôles.Personnaliser le style de fenêtre WPF

App.xaml

<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}"> 
    <Setter Property="WindowStyle" Value="None"/> 
    <Setter Property="AllowsTransparency" Value="True"/> 
    <Setter Property="ResizeMode" Value="NoResize"/> 
    <Setter Property="Background" Value="MintCream"/> 
    <Setter Property="BorderBrush" Value="#0046E7"/> 
    <Setter Property="BorderThickness" Value="2"/> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type Window}"> 
     <Grid Background="{TemplateBinding Background}"> 
      <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition Width="Auto"/> 
      </Grid.ColumnDefinitions> 
      <StackPanel Grid.ColumnSpan="2"> 
      <TextBlock TextAlignment="Center" 
         Margin="0 10 0 0" 
         FontSize="22" 
         FontWeight="DemiBold" 
         Foreground="RoyalBlue" 
         Text="{TemplateBinding Title}"/> 
      </StackPanel> 
      <StackPanel Grid.Row="0" Grid.Column="1" 
         Orientation="Horizontal" 
         HorizontalAlignment="Stretch" 
         VerticalAlignment="Center" 
         Margin="0 10 15 0"> 
      <Button Style="{StaticResource MinimizeButtonStyle}" 
        Width="25" 
        Height="22" 
        Margin="0 0 10 0"/> 
      <Button Style="{StaticResource CloseButtonStyle}" 
        Width="25" 
        Height="22"/> 
      </StackPanel> 
     </Grid> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 

MainWindow.xaml

<Window x:Class="WindowForHW2.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:WindowForHW2" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525" 
    Style="{StaticResource CustomWindowStyle}"> 
<Grid> 
    <Button Width="100" Height="40" Content="Hello"/> 
</Grid> 

Works modèle, mais je ne peux pas ajouter plus lissée: Window:

Répondre

4

Vous devez ajouter un ContentPresenter où le Content de votre Window va. Essaye ça.

<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}"> 
    <Setter Property="WindowStyle" Value="None"/> 
    <Setter Property="AllowsTransparency" Value="True"/> 
    <Setter Property="ResizeMode" Value="NoResize"/> 
    <Setter Property="Background" Value="MintCream"/> 
    <Setter Property="BorderBrush" Value="#0046E7"/> 
    <Setter Property="BorderThickness" Value="2"/> 
    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type Window}"> 
     <Grid Background="{TemplateBinding Background}"> 
      <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition Width="Auto"/> 
      </Grid.ColumnDefinitions> 
      <StackPanel Grid.ColumnSpan="2"> 
      <TextBlock TextAlignment="Center" 
         Margin="0 10 0 0" 
         FontSize="22" 
         FontWeight="DemiBold" 
         Foreground="RoyalBlue" 
         Text="{TemplateBinding Title}"/> 
      </StackPanel> 
      <StackPanel Grid.Row="0" Grid.Column="1" 
         Orientation="Horizontal" 
         HorizontalAlignment="Stretch" 
         VerticalAlignment="Center" 
         Margin="0 10 15 0"> 
      <Button Content="+" 
        Width="25" 
        Height="22" 
        Margin="0 0 10 0"/> 
      <Button Content="X" 
        Width="25" 
        Height="22" /> 
      </StackPanel> 
     <!-- here goes the content --> 
     <ContentPresenter Grid.Row="1"/> 
     </Grid> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 
+1

Cela fonctionne! Je t'aime. Je vous remercie – CepBuch