2015-08-16 131 views
0

全部, 我正在嘗試爲窗口設置模板。我想要的是根據窗口的狀態來改變邊距和邊框厚度。我正在嘗試使用Trigger,但是當我運行應用程序時,觸發器不會觸發。我的問題是,當窗口狀態改變時,如何讓觸發器觸發?WPF ControlTemplate觸發器

的XAML我是(我已刪除的大部分代碼保持張貼簡單):

<Window x:Class="WpfApplication1.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:WpfApplication1" Background="#FFE8E8E8" 
     Style="{DynamicResource ChromeWindowStyle}" 
     mc:Ignorable="d" 
     Title="BaseWindow" Width="500" Height="300"> 

    <WindowChrome.WindowChrome> 
     <WindowChrome ResizeBorderThickness="6" CaptionHeight="0" GlassFrameThickness="0" /> 
    </WindowChrome.WindowChrome> 

    <Window.Resources> 
     <Style x:Key="ChromeWindowStyle" TargetType="{x:Type Window}"> 
      <Setter Property="Foreground" Value="Black"/> 
      <Setter Property="Background" Value="#FFE8E8E8"/> 
      <Setter Property="WindowStyle" Value="None" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Window}"> 
         <Border x:Name="MainBorder" Background="White" BorderBrush="Gray" BorderThickness="1"> 
          <DockPanel LastChildFill="True"> 
           <Border Height="30" DockPanel.Dock="Top"> 
            <DockPanel LastChildFill="False"> 
             <TextBlock Text="Heading" DockPanel.Dock="Left" VerticalAlignment="Center" /> 
             <Button x:Name="CloseButton" Content="Close" DockPanel.Dock="Right" /> 
             <Button x:Name="MaximizeButton" Content="Max" DockPanel.Dock="Right" /> 
             <Button x:Name="MinButton" Content="Min" DockPanel.Dock="Right"/> 
            </DockPanel> 
           </Border> 
           <AdornerDecorator DockPanel.Dock="Bottom"> 
            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" 
                Content="{TemplateBinding Content}" 
                ContentStringFormat="{TemplateBinding ContentStringFormat}"/> 
           </AdornerDecorator> 
          </DockPanel> 
         </Border> 



         <!--This piece wont trigger - start --> 
         <ControlTemplate.Triggers> 
          <Trigger Property="WindowState" Value="Normal"> 
           <Setter Property="Margin" Value="8" /> 
           <Setter Property="BorderThickness" Value="1" /> 
          </Trigger> 
          <Trigger Property="WindowState" Value="Maximized"> 
           <Setter Property="Margin" Value="8" /> 
           <Setter Property="BorderThickness" Value="0" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
         <!--This piece wont trigger - end --> 



        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 

    <Grid> 

    </Grid> 
</Window> 

回答

0

你ControlTemplate.Triggers試圖在控件模板本身,設置屬性,這在 - 和 - 本身不是一個視覺元素。他們需要明確指定的邊境控制的名稱:

<ControlTemplate.Triggers> 
    <Trigger Property="WindowState" Value="Normal"> 
     <Setter TargetName="MainBorder" Property="Margin" Value="8" /> 
     <Setter TargetName="MainBorder" Property="BorderThickness" Value="1" /> 
    </Trigger> 
    <Trigger Property="WindowState" Value="Maximized"> 
     <Setter TargetName="MainBorder" Property="Margin" Value="8" /> 
     <Setter TargetName="MainBorder" Property="BorderThickness" Value="0" /> 
    </Trigger> 
</ControlTemplate.Triggers> 

編輯:那好吧,我仍然在4.5.1,也許這就是新的行爲?在這種情況下,我會走另外一條道路,即把DataTriggers在邊境管制本身:

<Style x:Key="ChromeWindowStyle" TargetType="{x:Type Window}"> 
     <Setter Property="Foreground" Value="Black"/> 
     <Setter Property="WindowStyle" Value="None" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Window}"> 
        <Border BorderBrush="Gray"> 
         <Border.Style> 
          <Style TargetType="{x:Type Border}"> 
           <Setter Property="Margin" Value="8" /> 
           <Setter Property="BorderThickness" Value="1" /> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=WindowState}" Value="Maximized"> 
             <Setter Property="Margin" Value="8" /> 
             <Setter Property="BorderThickness" Value="0" /> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </Border.Style> 

         <DockPanel LastChildFill="True"> 
          <Border Height="30" DockPanel.Dock="Top"> 
           <DockPanel LastChildFill="False"> 
            <TextBlock Text="Heading" DockPanel.Dock="Left" VerticalAlignment="Center" /> 
            <Button x:Name="CloseButton" Content="Close" DockPanel.Dock="Right" /> 
            <Button x:Name="MaximizeButton" Content="Max" DockPanel.Dock="Right" Click="MaximizeButton_Click"/> 
            <Button x:Name="MinButton" Content="Min" DockPanel.Dock="Right"/> 
           </DockPanel> 
          </Border> 
          <AdornerDecorator DockPanel.Dock="Bottom"> 
           <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" 
               Content="{TemplateBinding Content}" 
               ContentStringFormat="{TemplateBinding ContentStringFormat}"/> 
          </AdornerDecorator> 
         </DockPanel> 
        </Border> 

       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
+0

編譯器會引發2個,用建議的代碼編譯時錯誤:targetName屬性不能在風格二傳手設置和 名稱「MainBorder」無法識別。任何想法? –

+0

奇怪,對我來說工作正常,你在什麼版本的.NET? –

+0

我正在使用.Net框架4.5.2 –