C# wpf 附加属性实现任意控件拖动调整大小

WPF拖动改变大小系列

第一节 Grid内控件拖动调整大小
第二节 Canvas内控件拖动调整大小
第三节 窗口拖动调整大小
第四节 附加属性实现拖动调整大小(本章)
第五章 拓展更多调整大小功能


文章目录

  • WPF拖动改变大小系列
  • 前言
  • 一、如何实现?
    • 1.定义附加属性
    • 2.整合功能
    • 3.关联功能
  • 二、完整代码
  • 三、使用示例
    • 1.拖动控件大小
    • 2.拖动窗口大小
  • 总结


前言

前面几节讲了控件拖动改变大小的几种方法,根据不同的布局可以有不同的实现方式。本节主要讲的是利用附加属性对这些方式进行封装,实现复用性,否则直接添加装饰器的方式需要写cs代码较为麻烦,且不方便移植,实现起来很不方便。通过将上述几节的所有功能整合到附加属性中,增加灵活度和适用性。

一、如何实现?

1.定义附加属性

定义附加属性IsResizeable,用于指示控件是否可调整大小,ResizeTemplate用指定拖动控件的样式,实现界面业务分离。
IsResizeable:

public static bool GetIsResizeable(DependencyObject obj)
{
    return (bool)obj.GetValue(IsResizeableProperty);
}
public static void SetIsResizeable(DependencyObject obj, bool value)
{
    obj.SetValue(IsResizeableProperty, value);
}

// Using a DependencyProperty as the backing store for IsResizeable.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsResizeableProperty =
    DependencyProperty.RegisterAttached("IsResizeable", typeof(bool), typeof(Resize), new PropertyMetadata(false));

ResizeTemplate:

public static ControlTemplate GetResizeTemplate(DependencyObject obj)
{
    return (ControlTemplate)obj.GetValue(ResizeTemplateProperty);
}

public static void SetResizeTemplate(DependencyObject obj, ControlTemplate value)
{
    obj.SetValue(ResizeTemplateProperty, value);
}

// Using a DependencyProperty as the backing store for ResizeTemplate.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty ResizeTemplateProperty =
    DependencyProperty.RegisterAttached("ResizeTemplate", typeof(ControlTemplate), typeof(Resize), new PropertyMetadata(null));

2.整合功能

系列文章中的前三章节都是讲述wpf的各种控件拖动改变大小的方法,都使用了Thumb以及其DragDelta事件,所以将它们整合到一起还是比较容易的。具体代码可参考前面四章的内容。
整合的方法有很多种比如使用Switch的方式:

private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
    switch (_resizeType)
    {
        case ResizeType.None:
            break;
        case ResizeType.Window:
         //Window拖动调整大小逻辑,参考第三节《窗口拖动调整大小》
            break;
        case ResizeType.Canvas:
        //Canvas内控件拖动调整大小逻辑,参考第二节《Canvas内控件拖动调整大小》
            break;    
        case ResizeType.Grid:
        case ResizeType.Others:
        //Grid或其他容器内控件拖动调整大小逻辑,参考第一节《Grid内控件拖动调整大小》
            break;
    }
}

3.关联功能

在附加属性IsResizeable的改变事件中关联拖动代码,判断不同的控件并注册事件。

public void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//1.查找Thumb,或生成Thumb,添加到用控件的装饰层
//2.注册Thumb事件
}

二、完整代码

https://download.csdn.net/download/u013113678/58844466


三、使用示例

1.拖动控件大小

(1)、默认样式
默认样式中,会给控件添加一个装饰器里面包含8个方位的透明控件用于拖动。
示例代码:
Grid中

 <Grid>
     <Rectangle  Width="120" Height="120" Fill="Wheat"  local:Resize.IsResizeable="True"/>
 Grid>

Canvas中

 <Canvas>
     <Rectangle  Width="120" Height="120" Fill="Wheat"  local:Resize.IsResizeable="True"/>
 Canvas>

其他容器中

 <StackPanel>
     <Rectangle  Width="120" Height="120" Fill="Wheat"  local:Resize.IsResizeable="True"/>
 StackPanel>

效果预览如下:
C# wpf 附加属性实现任意控件拖动调整大小_第1张图片
(2)、自定义样式
自定义样式使用规则为:
1、给ResizeTemplate属性赋值自定义的ControlTemplate。
2、ControlTemplate中二级控件的Thumb自动被用于拖动。
3、Thumb的数量不限,拖动方向由HorizontalAlignment和VerticalAlignment决定。
示例如下:

<Grid>
    <Rectangle  Width="120" Height="120" Fill="Wheat"  local:Resize.IsResizeable="True" local:Resize.ResizeTemplate="{DynamicResource PART_Resizeable_Thumbs}">
        <Rectangle.Resources>
            <ControlTemplate x:Key="PART_Resizeable_Thumbs">
                <Grid >
                    
                    <Thumb  Margin="-8,0,0,0" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Center" Cursor="SizeWE">
                        <Thumb.Template>
                            <ControlTemplate>
                                <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                            ControlTemplate>
                        Thumb.Template>
                    Thumb>
                    
                    <Thumb  Margin="0,-8,0,0" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Top" Cursor="SizeNS">
                        <Thumb.Template>
                            <ControlTemplate>
                                <Border BorderBrush="Gray"  BorderThickness="2" CornerRadius="8"  Background="White">Border>
                            ControlTemplate>
                        Thumb.Template>
                    Thumb>
                    
                    <Thumb  Margin="0,0,-8,0" Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Center" Cursor="SizeWE">
                        <Thumb.Template>
                            <ControlTemplate>
                                <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                            ControlTemplate>
                        Thumb.Template>
                    Thumb>
                    
                    <Thumb  Margin="0,0,0,-8" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Bottom" Cursor="SizeNS">
                        <Thumb.Template>
                            <ControlTemplate>
                                <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                            ControlTemplate>
                        Thumb.Template>
                    Thumb>
                    
                    <Thumb  Margin="-8,-8,0,0" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="SizeNWSE">
                        <Thumb.Template>
                            <ControlTemplate>
                                <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                            ControlTemplate>
                        Thumb.Template>
                    Thumb>
                    
                    <Thumb  Margin="0,-8,-8,0" Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Top"  Cursor="SizeNESW">
                        <Thumb.Template>
                            <ControlTemplate>
                                <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                            ControlTemplate>
                        Thumb.Template>
                    Thumb>
                    
                    <Thumb  Margin="0,0,-8,-8" Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Bottom"  Cursor="SizeNWSE">
                        <Thumb.Template>
                            <ControlTemplate>
                                <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White" >Border>
                            ControlTemplate>
                        Thumb.Template>
                    Thumb>
                    
                    <Thumb  Margin="-8,0,0,-8" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Bottom" Cursor="SizeNESW">
                        <Thumb.Template>
                            <ControlTemplate>
                                <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                            ControlTemplate>
                        Thumb.Template>
                    Thumb>
                Grid>
            ControlTemplate>
        Rectangle.Resources>
    Rectangle>
Grid>

效果预览:
C# wpf 附加属性实现任意控件拖动调整大小_第2张图片

2.拖动窗口大小

(1)、默认样式
代码:

<Window x:Class="WpfControlMove.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:WpfControlMove"
        mc:Ignorable="d"
        Title="MainWindow" Height="360" Width="640"                
        Background="Transparent"       
        WindowStyle="None"
        AllowsTransparency="True"
        local:Resize.IsResizeable="True"
        >
    <Grid  Margin="10" Background="White">
        <Grid.Effect>
            <DropShadowEffect Color="#FFAAAAAA" ShadowDepth="0" BlurRadius="10" Opacity="0.4" />
        Grid.Effect>
    Grid>
Window>

效果预览:C# wpf 附加属性实现任意控件拖动调整大小_第3张图片

(2)、自定义样式
自定义样式的方法上面以及说明,在此处不再赘述。
示例代码:

<Window x:Class="WpfControlMove.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:WpfControlMove"
        mc:Ignorable="d"
        Title="MainWindow" Height="360" Width="640"              
        Background="Transparent"    
        AllowsTransparency="True"
        WindowStyle="None"
        local:Resize.IsResizeable="True"  
        local:Resize.ResizeTemplate="{DynamicResource PART_Resizeable_Thumbs}"
        >
    <Window.Resources>
        <ControlTemplate x:Key="PART_Resizeable_Thumbs">
            <Grid >
                
                <Thumb  Margin="-8,0,0,0" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Center" Cursor="SizeWE">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                        ControlTemplate>
                    Thumb.Template>
                Thumb>
                
                <Thumb  Margin="0,-8,0,0" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Top" Cursor="SizeNS">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Border BorderBrush="Gray"  BorderThickness="2" CornerRadius="8"  Background="White">Border>
                        ControlTemplate>
                    Thumb.Template>
                Thumb>
                
                <Thumb  Margin="0,0,-8,0" Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Center" Cursor="SizeWE">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                        ControlTemplate>
                    Thumb.Template>
                Thumb>
                
                <Thumb  Margin="0,0,0,-8" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Bottom" Cursor="SizeNS">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                        ControlTemplate>
                    Thumb.Template>
                Thumb>

                
                <Thumb  Margin="-8,-8,0,0" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="SizeNWSE">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                        ControlTemplate>
                    Thumb.Template>
                Thumb>
                
                <Thumb  Margin="0,-8,-8,0" Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Top"  Cursor="SizeNESW">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                        ControlTemplate>
                    Thumb.Template>
                Thumb>
                
                <Thumb  Margin="0,0,-8,-8" Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Bottom"  Cursor="SizeNWSE">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White" >Border>
                        ControlTemplate>
                    Thumb.Template>
                Thumb>
                
                <Thumb  Margin="-8,0,0,-8" Width="16" Height="16" HorizontalAlignment="Left" VerticalAlignment="Bottom" Cursor="SizeNESW">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Border BorderBrush="Gray" BorderThickness="2" CornerRadius="8"  Background="White">Border>
                        ControlTemplate>
                    Thumb.Template>
                Thumb>
            Grid>
        ControlTemplate>
    Window.Resources>
    <Grid  Margin="10" Background="White">
        <Grid.Effect>
            <DropShadowEffect Color="#FFAAAAAA" ShadowDepth="0" BlurRadius="10" Opacity="0.4" />
        Grid.Effect>
    Grid>
Window>

效果预览:

C# wpf 附加属性实现任意控件拖动调整大小_第4张图片


总结

总得来说将拖动大小功能封装成一共附加属性是很有用的。首先是具有通用性,在不同的窗口以及不同的项目中都可以使用,同时也减少了代码维护量,修改时只需要集中改一出即可,然后使用方法也是比较简单直接。

你可能感兴趣的:(#,wpf,wpf,c#,microsoft,开发语言,xml)