自定义WPF-WindowStyle:如何识别单击自定义帮助按钮?

时间:2014-09-22 14:28:34

标签: wpf events styles

我的WPF Windows有自定义样式。其中一部分是常规最小化,最大化和关闭按钮旁边的附加HelpButton。将此样式分配给我的Windows按预期工作,但我的窗口如何通知单击此按钮?按钮是在样式中定义的,所以我的窗口并不真正了解它。这是样式的代码:

    <ResourceDictionary x:Class="Company.Common.Themes.CompanyWindowStyle"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/Company.Common.Themes;component/CompanyWindowBaseStyle.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style x:Key="CompanyWindowStyle"
           BasedOn="{StaticResource CompanyWindowBaseStyle}"
           TargetType="{x:Type Window}">
        <Setter Property="ResizeMode"
                Value="CanResize" />
        <EventSetter Event="Loaded"
                     Handler="WindowLoaded" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border x:Name="PART_Container"
                            Padding="7,7,7,5">
                        <Grid TextOptions.TextFormattingMode="Display"
                              TextOptions.TextRenderingMode="ClearType">
                            <Border x:Name="PART_Border"
                                    Width="Auto"
                                    Height="Auto"
                                    Background="{StaticResource BackgroundBrush}"
                                    BorderBrush="{StaticResource BorderBrush}"
                                    BorderThickness="1">
                                <DockPanel HorizontalAlignment="Stretch"
                                           VerticalAlignment="Stretch"
                                           Background="Transparent">
                                    <Border x:Name="TitleBar"
                                            Background="{StaticResource BackgroundBrush}"
                                            BorderThickness="0"
                                            DockPanel.Dock="Top"
                                            MouseDown="TitleBarMouseDown">
                                        <Grid Height="32"
                                              Background="Transparent">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="36" />
                                                <ColumnDefinition />
                                                <ColumnDefinition Width="34" />
                                                <ColumnDefinition Width="34" />
                                                <ColumnDefinition Width="34" />
                                                <ColumnDefinition Width="34" />
                                            </Grid.ColumnDefinitions>
                                            <ContentControl Grid.Column="0"
                                                            MouseDoubleClick="IconMouseDoubleClicked">
                                                <Image x:Name="Icon"
                                                       Width="32"
                                                       Height="32"
                                                       Margin="4,-7,0,7"
                                                       HorizontalAlignment="Right"
                                                       WindowChrome.IsHitTestVisibleInChrome="True"
                                                       Source="{Binding Path=Icon,
                                                                    Mode=OneWay,
                                                                    RelativeSource={RelativeSource TemplatedParent}}" />
                                            </ContentControl>
                                            <TextBlock x:Name="Caption"
                                                       Grid.Column="1"
                                                       Margin="4,0,0,0"
                                                       HorizontalAlignment="Center"
                                                       VerticalAlignment="Center"
                                                       FontFamily="Segoe UI"
                                                       FontSize="12"
                                                       Opacity="0.66"
                                                       Text="{Binding Path=Title,
                                                                      Mode=OneWay,
                                                                      RelativeSource={RelativeSource TemplatedParent}}" />
                                            <Button x:Name="HelpButton"
                                                    Grid.Column="2"
                                                    Width="34"
                                                    Height="26"
                                                    VerticalAlignment="Top"
                                                    Click="HelpButtonClick"
                                                    Style="{StaticResource TitleBarButtonStyle}"
                                                    WindowChrome.IsHitTestVisibleInChrome="True">
                                                <Grid MaxWidth="9"
                                                      MaxHeight="9">
                                                    <TextBlock Text="?" FontSize="14"
                                                               Foreground="{Binding RelativeSource={RelativeSource AncestorType=Button},
                                                                           Path=Foreground}" />
                                                </Grid>
                                            </Button>
                                            <Button x:Name="MinButton"
                                                Grid.Column="3"
                                                Width="34"
                                                Height="26"
                                                VerticalAlignment="Top"
                                                Click="MinButtonClick"
                                                Style="{StaticResource TitleBarButtonStyle}"
                                                WindowChrome.IsHitTestVisibleInChrome="True">
                                            <Grid MaxWidth="9"
                                                  MaxHeight="9">
                                                <Path Data="M0,8 H8 M0,7 H8 M0,6 H8"
                                                      RenderOptions.EdgeMode="Aliased"
                                                      Stretch="None"
                                                      Stroke="{Binding RelativeSource={RelativeSource AncestorType=Button},
                                                                       Path=Foreground}"
                                                      StrokeThickness="1" />
                                            </Grid>
                                        </Button>
                                            <Button x:Name="MaxButton"
                                                Grid.Column="4"
                                                Width="34"
                                                Height="26"
                                                VerticalAlignment="Top"
                                                Click="MaxButtonClick"
                                                Style="{StaticResource TitleBarButtonStyle}"
                                                WindowChrome.IsHitTestVisibleInChrome="True">
                                            <Grid MaxWidth="9"
                                                  MaxHeight="9">
                                                <Path x:Name="PART_MaxButton_Path"
                                                      Data="M0,0 H8 V8 H0 V0 M0,1 H8 M0,2 H8"
                                                      RenderOptions.EdgeMode="Aliased"
                                                      Stretch="None"
                                                      Stroke="{Binding RelativeSource={RelativeSource AncestorType=Button},
                                                                       Path=Foreground}"
                                                      StrokeThickness="1" />
                                            </Grid>
                                        </Button>
                                            <Button x:Name="CloseButton"
                                                Grid.Column="5"
                                                Width="34"
                                                Height="26"
                                                VerticalAlignment="Top"
                                                Click="CloseButtonClick"
                                                Style="{StaticResource TitleBarButtonStyle}"
                                                WindowChrome.IsHitTestVisibleInChrome="True">
                                            <Grid MaxWidth="9"
                                                  MaxHeight="9">
                                                <Path Data="M0,0 L8,8 M0,8 L8,0"
                                                      Stretch="None"
                                                      Stroke="{Binding RelativeSource={RelativeSource AncestorType=Button},
                                                                       Path=Foreground}"
                                                      StrokeThickness="1.5" />
                                            </Grid>
                                        </Button>
                                        </Grid>
                                    </Border>
                                    <ContentPresenter />
                                </DockPanel>
                            </Border>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                    <Trigger Property="WindowState"
                             Value="Maximized">
                        <Setter TargetName="PART_MaxButton_Path"
                                Property="Data"
                                Value="M0,3 H5 V8 H0 V3 M0,4 H5 M3,0 H8 V5 H7 M3,1 H8" />
                    </Trigger>
                    <Trigger Property="WindowState"
                             Value="Normal">
                        <Setter TargetName="PART_Border"
                                Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect BlurRadius="7"
                                                  Direction="315"
                                                  Opacity="0.5"
                                                  ShadowDepth="2"
                                                  Color="black" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsActive"
                             Value="False">
                        <Setter TargetName="PART_Border"
                                Property="BorderBrush"
                                Value="{StaticResource BorderBrushInactive}" />
                    </Trigger>
                    <Trigger Property="IsActive"
                             Value="True">
                        <Setter TargetName="PART_Border"
                                Property="BorderBrush"
                                Value="{StaticResource BorderBrush}" />
                    </Trigger>
                </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

我不知道怎么做。有人可以指出我正确的方向吗?

更新: 这是我使用Style:

的MainWindow XAML的标题
<Window x:Class="Company.MainGUI.GUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="clr-namespace:Company.Common;assembly=Company.Common"
xmlns:controls="clr-namespace:Company.Common.Controls;assembly=Company.Common.Controls"
xmlns:l="clr-namespace:Company.Common.Languages;assembly=Company.Common.Languages"
xmlns:local="clr-namespace:Company.MainGUI.GUI"
Title="{x:Static l:Common_Text.Application_Name}"
UseLayoutRounding="True"
MinWidth="900"
MinHeight="600"
Icon="pack://application:,,,/Company.Common.Images;component/Resources/ApplicationLogo_MainGUI.ico"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Company.Common.Themes;component/CompanyWindowStyle.xaml" />
<ResourceDictionary Source="pack://application:,,,/Company.Common.Themes;component/MainGUISkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Window.Style>
<Style TargetType="{x:Type local:MainWindow}"
BasedOn="{StaticResource CompanyWindowStyle}" />
</Window.Style>

0 个答案:

没有答案