窗口模板的ResourceDictionary中的WPF事件
我目前正在尝试实现Metro样式的窗口。窗口模板的ResourceDictionary中的WPF事件,wpf,events,event-handling,resourcedictionary,Wpf,Events,Event Handling,Resourcedictionary,我目前正在尝试实现Metro样式的窗口。 因此,我在Resourcedictionary中制作了以下样式: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!-- Brushes --> <
因此,我在Resourcedictionary中制作了以下样式:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Brushes -->
<SolidColorBrush x:Key="BackgroundColor" Color="#FFFFFFFF" />
<!-- Buttons -->
<Style x:Key="MetroControlBoxButton" TargetType="Button">
<Setter Property="Background" Value="{StaticResource BackgroundColor}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Windows -->
<Style x:Key="MetroWindow" TargetType="Window">
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Window">
<Grid Background="{StaticResource BackgroundColor}">
<Grid.RowDefinitions>
<RowDefinition Height="6" />
<RowDefinition Height="24" />
<RowDefinition Height="*" />
<RowDefinition Height="24" />
<RowDefinition Height="6" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="6" />
</Grid.ColumnDefinitions>
<Rectangle Name="topLeftBorderRectangle" Fill="Red" Grid.Row="0" Grid.Column="0" />
<Rectangle Name="topCenterBorderRectangle" Fill="Orange" Grid.Row="0" Grid.Column="1" />
<Rectangle Name="topRightBorderRectangle" Fill="Red" Grid.Row="0" Grid.Column="2" />
<Rectangle Name="middleLeftBorderRectangle" Fill="Orange" Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" />
<Rectangle Name="middleRightBorderRectangle" Fill="Orange" Grid.Row="1" Grid.RowSpan="3" Grid.Column="2" />
<Rectangle Name="bottomLeftBorderRectangle" Fill="Red" Grid.Row="4" Grid.Column="0" />
<Rectangle Name="bottomCenterBorderRectangle" Fill="Orange" Grid.Row="4" Grid.Column="1" />
<Rectangle Name="bottomRightBorderRectangle" Fill="Red" Grid.Row="4" Grid.Column="2" />
<Rectangle Name="statusBarRectangle" Fill="Yellow" Grid.Row="3" Grid.Column="1" />
<Grid Grid.Row="1" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
</Grid.ColumnDefinitions>
<Rectangle Name="dragRectangle" Fill="Yellow" Grid.Row="0" Grid.Column="1" />
<Button Name="minimizeButton" Content="_" Grid.Row="0" Grid.Column="2" Style="{StaticResource MetroControlBoxButton}" />
<Button Name="maximizeButton" Content="[]" Grid.Row="0" Grid.Column="3" Style="{StaticResource MetroControlBoxButton}" />
<Button Name="closeButton" Content="X" Grid.Row="0" Grid.Column="4" Style="{StaticResource MetroControlBoxButton}" />
</Grid>
<ContentPresenter Grid.Row="2" Grid.Column="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我的问题是我想实现控制盒按钮(最小化、最大化和关闭)。我可以创建三个按钮,将它们右上对齐。 但是由于我的风格是在ResourceDictionary中,我不知道如何实现单击事件 调整窗口大小和拖动窗口也是如此。我知道如何拖动和调整窗口大小,但我需要将其链接到Window\u MouseLeftButtonDown事件,但我不知道如何在模板内链接它 我看到我“可以”创建一个类并将代码放在这里:
但这似乎不是一个好办法 我想我可以创建一个MetroWindow,并让我的其他窗口继承它。 但是WPF不支持可视化继承 那么,在整个应用程序中,在多个窗口上重用窗口模板的最佳做法是什么呢 谢谢,
SiriusNik我对按钮问题有点困惑。即使您已经在样式的Template属性中创建了一个按钮,您仍然需要将该样式应用于我在您的示例中没有看到的button对象。单击事件或命令将添加到按钮元素 例如:
<Button x:Name="testButton" Style="{StaticResource MetroControlBoxButton}" ... />
我已经更新了我原来的帖子,添加了按钮。那么我的closeButton如何执行Close();方法?即使它在模板中,因为您已经定义了按钮,所以可以执行命令绑定以执行位于窗口DataContext中的操作。我相信这是唯一可以做到这一点的方法,因为如果在ResourceDictionary中定义,则不会找到该事件。如果我错了,请有人纠正我。我更新了下面的答案,以展示一个例子。从你的答案中,我意识到我缺少一些知识。经过一些研究,我发现我对MVVM一无所知!因此,我制作了一个MetroWindowViewModel,并成功地绑定了该命令。但有一个问题,如果我想在多个窗口上使用我的窗口模板,我是否需要让每个新的Windows ModelView继承我的MetroWindowModelView?您不必继承该ViewModel。您可以附加它的另一个实例,甚至可以附加一个单独的ViewModel,只要它有一个同名的命令,窗口就可以绑定到它。@SiriusNik我遇到了与您相同的问题,现在您能回答这个问题吗?:D我搜索了很多,但没有结果
Command="{Binding Path=DataContext.ClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"