Wpf 在运行时创建可调整大小的面板
我试图让用户在运行时调整网格的大小。我发现了很多例子,但它们似乎都使使用装饰物等变得过于复杂Wpf 在运行时创建可调整大小的面板,wpf,xaml,user-interface,expression-blend,Wpf,Xaml,User Interface,Expression Blend,我试图让用户在运行时调整网格的大小。我发现了很多例子,但它们似乎都使使用装饰物等变得过于复杂 我只想在右下角使用一个简单的控件,例如thumb或ResizeGrip,这将使用户能够调整面板的大小。您可以使用拇指来计算调整大小逻辑,并覆盖ContentPresenter的样式,然后您可以将网格添加到ContentPresenter 工作示例: 代码: 命名空间WpfApplication12 { /// ///MainWindow.xaml的交互逻辑 /// 公共部分类主窗口:窗口 { 公共主
我只想在右下角使用一个简单的控件,例如thumb或ResizeGrip,这将使用户能够调整面板的大小。您可以使用
拇指
来计算调整大小逻辑,并覆盖ContentPresenter
的样式
,然后您可以将网格
添加到ContentPresenter
工作示例:
代码:
命名空间WpfApplication12
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
}
公共类ResizeThumb:Thumb
{
公众辞职
{
DragDelta+=新的DragDeltaEventHandler(this.resizetumb_DragDelta);
}
私有void resizetumb_DragDelta(对象发送方,DragDeltaEventArgs e)
{
Control designerItem=this.DataContext作为控件;
如果(designerItem!=null)
{
双三角洲垂直,三角洲水平;
开关(垂直对齐)
{
外壳垂直对齐。底部:
deltaVertical=数学最小值(-e.垂直变化,designerItem.ActualHeight-designerItem.MinHeight);
设计项目高度-=三角高程;
打破
外壳垂直对齐。顶部:
deltaVertical=Math.Min(例如垂直变化、designerItem.ActualHeight-designerItem.MinHeight);
Canvas.SetTop(designerItem,Canvas.GetTop(designerItem)+deltaVertical);
设计项目高度-=三角高程;
打破
违约:
打破
}
开关(水平对齐)
{
案例水平对齐。左:
deltaHorizontal=Math.Min(例如水平变化、designerItem.ActualWidth-designerItem.MinWidth);
Canvas.SetLeft(designerItem,Canvas.GetLeft(designerItem)+deltaHorizontal);
设计项目宽度-=水平三角形;
打破
案例水平对齐。右侧:
deltaHorizontal=数学.Min(-e.水平变化,designerItem.ActualWidth-designerItem.MinWidth);
设计项目宽度-=水平三角形;
打破
违约:
打破
}
}
e、 已处理=正确;
}
}
}
Xaml:
结果:
您可以修改以下内容:嗨,sa_ddam213,非常感谢您的回答。请原谅,因为我对这一点很陌生,但当我尝试构建它时,我会遇到很多错误,例如:名称“ResizeThumb”不存在于命名空间“clr namespace:WpfApplication12”&类型为“ResizeThumb”的值不能添加到类型为“UIElementCollection”的集合或字典中。您好,它确实有效,其他人帮我把它组装起来。再次感谢您的回答。使用.Net Core时,一定有命名冲突。我必须完全限定对齐方式,例如
System.Windows.HorizontalAlignment.Left
。
namespace WpfApplication12
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class ResizeThumb : Thumb
{
public ResizeThumb()
{
DragDelta += new DragDeltaEventHandler(this.ResizeThumb_DragDelta);
}
private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Control designerItem = this.DataContext as Control;
if (designerItem != null)
{
double deltaVertical, deltaHorizontal;
switch (VerticalAlignment)
{
case VerticalAlignment.Bottom:
deltaVertical = Math.Min(-e.VerticalChange, designerItem.ActualHeight - designerItem.MinHeight);
designerItem.Height -= deltaVertical;
break;
case VerticalAlignment.Top:
deltaVertical = Math.Min(e.VerticalChange, designerItem.ActualHeight - designerItem.MinHeight);
Canvas.SetTop(designerItem, Canvas.GetTop(designerItem) + deltaVertical);
designerItem.Height -= deltaVertical;
break;
default:
break;
}
switch (HorizontalAlignment)
{
case HorizontalAlignment.Left:
deltaHorizontal = Math.Min(e.HorizontalChange, designerItem.ActualWidth - designerItem.MinWidth);
Canvas.SetLeft(designerItem, Canvas.GetLeft(designerItem) + deltaHorizontal);
designerItem.Width -= deltaHorizontal;
break;
case HorizontalAlignment.Right:
deltaHorizontal = Math.Min(-e.HorizontalChange, designerItem.ActualWidth - designerItem.MinWidth);
designerItem.Width -= deltaHorizontal;
break;
default:
break;
}
}
e.Handled = true;
}
}
<Window x:Class="WpfApplication12.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication12"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<Control x:Name="resizer">
<Control.Style>
<Style TargetType="{x:Type Control}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<Grid Margin="-3">
<local:ResizeThumb Width="7" Height="7" Margin="-2" Cursor="SizeNWSE" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Control.Style>
</Control>
<ContentPresenter Content="{TemplateBinding Content}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Canvas>
<ContentControl Width="200" Height="100" Canvas.Left="10" Canvas.Top="10" >
<Grid Background="Blue">
<TextBlock Text="ResizeGrid" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Red" />
</Grid>
</ContentControl>
</Canvas>
</Window>