gpt4 book ai didi

ControlTemplate 的 ResourceDictionary 中的 WPF 事件

转载 作者:行者123 更新时间:2023-12-03 10:37:03 24 4
gpt4 key购买 nike

我目前正在尝试实现一个 Metro 风格的窗口。
所以我在 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}" Command="{Binding Path=DataContext.MinimizeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<Button Name="maximizeButton" Content="[]" Grid.Row="0" Grid.Column="3" Style="{StaticResource MetroControlBoxButton}" Command="{Binding Path=DataContext.MaximizeNormalizeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<Button Name="closeButton" Content="X" Grid.Row="0" Grid.Column="4" Style="{StaticResource MetroControlBoxButton}" Command="{Binding Path=DataContext.CloseCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</Grid>

<ContentPresenter Grid.Row="2" Grid.Column="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

</ResourceDictionary>

我的问题是我不知道如何实现 de drag 功能。
我的 dragRectangle 没有 Command 属性,那么如何使用 MVVM 在 Rectangle 上的 MouseLeftButtonDown 上调用 DragMove()?

谢谢

最佳答案

ResourceDictionary 可以像 Windows 等一样包含代码,因此您可以添加事件处理程序并调用 DragMove从那里

设置后面的代码需要几个步骤。

  • 如果您的 ResourceDictionary 名为 MetroStyleResourceDictionary.xaml在 Visual Studio 中添加一个名为 MetroStyleResourceDictionary.xaml.cs 的相同文件夹中的新文件
  • 文件后面的代码应该如下所示
    public partial class MetroStyleResourceDictionary
    {
    //...
    }
  • 之后您需要添加 x:Class Xaml 文件的属性
    <ResourceDictionary x:Class="YourNamespace.MetroStyleResourceDictionary"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!--...-->
    </ResourceDictionary>

  • 现在您可以为 MouseLeftButtonDown 添加一个事件处理程序到dragRectangle。 .您还需要获取 Window所以绑定(bind)到 Tag可能是个好主意
    <Rectangle Name="dragRectangle"
    MouseLeftButtonDown="dragRectangle_MouseLeftButtonDown"
    Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
    .../>

    最后,您可以将事件处理程序添加到文件后面的代码中,如下所示
    public partial class MetroStyleResourceDictionary
    {
    void dragRectangle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
    Rectangle dragRectangle = sender as Rectangle;
    Window window = dragRectangle.Tag as Window;
    if (window != null)
    {
    window.DragMove();
    }
    }
    }

    关于ControlTemplate 的 ResourceDictionary 中的 WPF 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7045718/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com