赞
踩
第一节 Grid内控件拖动调整大小
第二节 Canvas内控件拖动调整大小
第三节 窗口拖动调整大小
第四节 附加属性实现拖动调整大小(本章)
第五章 拓展更多调整大小功能
定义附加属性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));
系列文章中的前三章节都是讲述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; } }
在附加属性IsResizeable的改变事件中关联拖动代码,判断不同的控件并注册事件。
public void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//1.查找Thumb,或生成Thumb,添加到用控件的装饰层
//2.注册Thumb事件
}
https://download.csdn.net/download/u013113678/58844466
(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>
效果预览如下:
(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>
效果预览:
(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>
效果预览:
(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>
效果预览:
总得来说将拖动大小功能封装成一共附加属性是很有用的。首先是具有通用性,在不同的窗口以及不同的项目中都可以使用,同时也减少了代码维护量,修改时只需要集中改一出即可,然后使用方法也是比较简单直接。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。