首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ControlTemplate的ResourceDictionary中的WPF事件

ControlTemplate的ResourceDictionary中的WPF事件
EN

Stack Overflow用户
提问于 2011-08-13 04:05:40
回答 1查看 15.6K关注 0票数 26

我目前正在尝试实现一个Metro样式的窗口。

因此,我在ResourceDictionary中创建了以下样式:

代码语言:javascript
复制
<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没有命令属性,那么我如何使用MVVM在矩形的MouseLeftButtonDown上调用DragMove()呢?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-13 04:34:47

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

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

  • 如果您的代码名为ResourceDictionary,则在Visual Studio中的名为MetroStyleResourceDictionary.xaml的同一文件夹中添加一个新文件,代码隐藏文件应如下所示

公共分部类MetroStyleResourceDictionary { //... }

  • 之后,您需要将x:Class属性添加到Xaml文件中

现在,您可以向MouseLeftButtonDown的dragRectangle添加事件处理程序。您还需要掌握Window,以便绑定到Tag可能是个好主意

代码语言:javascript
复制
<Rectangle Name="dragRectangle"
           MouseLeftButtonDown="dragRectangle_MouseLeftButtonDown"
           Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
           .../>

最后,您可以将事件处理程序添加到代码隐藏文件中,如下所示

代码语言:javascript
复制
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();
        }
    }
}
票数 46
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7045718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档