WPF中得Panel(含StackPanel)子元素之间间隔不可通过属性设置修改,而即使是设置Style的方式,也只有子元素类型相同时才可修改,因此加入附加属性很有必要。
附加属性源码
如何添加附加属性
其中,CodeProject上的这位老哥代码稍微有点问题,As那里跑不下去,修改如下:
using System.Windows;
using System.Windows.Controls;
namespace Extensions
public static class PanelExtensions
#region HorizontalContentAlignment
public static readonly DependencyProperty HorizontalContentAlignmentProperty =
DependencyProperty.RegisterAttached("HorizontalContentAlignment",
typeof(HorizontalAlignment), typeof(PanelExtensions),
new FrameworkPropertyMetadata(HorizontalAlignment.Left,
OnHorizontalContentAlignmentChanged));
public static void SetHorizontalContentAlignment(Panel d, HorizontalAlignment value)
d.SetValue(HorizontalContentAlignmentProperty, value);
public static HorizontalAlignment GetHorizontalContentAlignment(Panel d)
return (HorizontalAlignment)d.GetValue(HorizontalContentAlignmentProperty);
static void OnHorizontalContentAlignmentChanged
(object sender, DependencyPropertyChangedEventArgs e)
var Panel = sender as Panel;
Panel.SizeChanged -= OnHorizontalContentAlignmentUpdated;
Panel.SizeChanged += OnHorizontalContentAlignmentUpdated;
OnHorizontalContentAlignmentUpdated(Panel, null);
static void OnHorizontalContentAlignmentUpdated(object sender, SizeChangedEventArgs e)
var p = sender as Panel;
var a = GetHorizontalContentAlignment(p);
for (int i = 0, Count = p.Children.Count; i < Count; i++)
(p.Children[i] as FrameworkElement).HorizontalAlignment = a;
#endregion
#region Spacing
public static readonly DependencyProperty SpacingProperty =
DependencyProperty.RegisterAttached("Spacing", typeof(Thickness),
typeof(PanelExtensions), new FrameworkPropertyMetadata(default(Thickness), OnSpacingChanged));
public static void SetSpacing(Panel d, Thickness value)
d.SetValue(SpacingProperty, value);
public static Thickness GetSpacing(Panel d)
return (Thickness)d.GetValue(SpacingProperty);
static void OnSpacingChanged(object sender, DependencyPropertyChangedEventArgs e)
var Panel = sender as Panel;
Panel.SizeChanged -= OnSpacingUpdated;
Panel.SizeChanged += OnSpacingUpdated;
OnSpacingUpdated(Panel, null);
static void OnSpacingUpdated(object sender, SizeChangedEventArgs e)
var p = sender as Panel;
var s = GetSpacing(p);
var tf = GetTrimFirst(p);
var tl = GetTrimLast(p);
for (int i = 0, Count = p.Children.Count; i < Count; i++)
var Element = p.Children[i] as FrameworkElement;
if ((i == 0 && tf) || (i == (Count - 1) && tl))
Element.Margin = new Thickness(0);
continue;
Element.Margin = s;
#endregion
#region TrimFirst
public static readonly DependencyProperty TrimFirstProperty =
DependencyProperty.RegisterAttached("TrimFirst", typeof(bool), typeof(PanelExtensions),
new FrameworkPropertyMetadata(false, OnSpacingChanged));
public static void SetTrimFirst(Panel d, bool value)
d.SetValue(TrimFirstProperty, value);
public static bool GetTrimFirst(Panel d)
return (bool)d.GetValue(TrimFirstProperty);
#endregion
#region TrimLast
public static readonly DependencyProperty TrimLastProperty =
DependencyProperty.RegisterAttached("TrimLast", typeof(bool), typeof(PanelExtensions),
new FrameworkPropertyMetadata(false, OnSpacingChanged));
public static void SetTrimLast(Panel d, bool value)
d.SetValue(TrimLastProperty, value);
public static bool GetTrimLast(Panel d)
return (bool)d.GetValue(TrimLastProperty);
#endregion
#region VerticalContentAlignment
public static readonly DependencyProperty VerticalContentAlignmentProperty =
DependencyProperty.RegisterAttached("VerticalContentAlignment", typeof(VerticalAlignment),
typeof(PanelExtensions), new FrameworkPropertyMetadata(VerticalAlignment.Top,
OnVerticalContentAlignmentChanged));
public static void SetVerticalContentAlignment(Panel d, VerticalAlignment value)
d.SetValue(VerticalContentAlignmentProperty, value);
public static VerticalAlignment GetVerticalContentAlignment(Panel d)
return (VerticalAlignment)d.GetValue(VerticalContentAlignmentProperty);
static void OnVerticalContentAlignmentChanged(object sender, DependencyPropertyChangedEventArgs e)
var Panel = sender as Panel;
Panel.SizeChanged -= OnVerticalContentAlignmentUpdated;
Panel.SizeChanged += OnVerticalContentAlignmentUpdated;
OnVerticalContentAlignmentUpdated(Panel, null);
static void OnVerticalContentAlignmentUpdated(object sender, SizeChangedEventArgs e)
var p = sender as Panel;
var a = GetVerticalContentAlignment(p);
for (int i = 0, Count = p.Children.Count; i < Count; i++)
(p.Children[i] as FrameworkElement).VerticalAlignment = a;
#endregion
三. WrapPanel
WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列,当长度或高度不够是就会自动调整进行换行,后续排序按照从上至下或从右至左的顺序进行。
Orientation——根据内容自动换行。当 Horizontal选项看上去类似于Windows资源管理器的缩略图视图:元素是...
前几天写了文章:WPF中的DataGrid控件的VerticalScrollBarVisibility属性失效
今天继续补充StackPanel的特点,可以作为对上述文章的进一步解释。
在WPF中,StackPanel是十分常用的布局元素。然而,该元素和很多其它元素不同,当其内部元素需要的尺寸较大时(超出StackPanel父元素)的尺寸时,如果没有明确限制StackPanel元素的Ma...
注意:Margin是边距
<Window x:Class="
WpfApp1.Window1"
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"
WPF布局——布局面板WrapPanelWPF——WrapPanel布局控件WrapPanel实例——十个按钮
WPF——WrapPanel布局控件
WrapPanel(自动折行面板),允许任意多的子元素按照声明的先后顺序,从左往右摆放,摆满一行后,自动折行。折行面板的经典的例子就是工具条布局。
默认情况下,WrapPanel根据子元素的内容,自动调整控件的大小。也可以设置ItemWidth和ItemHeight属性来约束子元素的宽度和高度。
WrapPanel实例——十个按钮
此例设计要求:现在使用自动折
应用程序界面设计中,合理的元素布局至关重要,它可以方便用户使用,并将信息清晰合理地展现给用户。WPF提供了一套功能强大的工具-面板(Panel),来控制用户界面的布局。你可以使用这些面板控件来排布元素。如果内置布局控件不能满足需要的话,还可以创建自定义的布局元素。
面板(Panel)WPF用于布局的面板主要有6个,StackPanel(栈面板)、WrapPanel(环绕面板)。DockPanel...
1、TabItem表示TabControl控件中的一页。TabItem类添加的唯一有意义的属性是IsSelected,该属性指示选项卡当前是否显示在TabControl控件中。
2、可使用TabStripPlacement属性,使各个选项卡在选项卡控件的侧边显示,而不是在正常的顶部位置显示。
3、将控件拖入表格
属性Dock为Fill;
4、更改控件跨行和跨列的
属性columnspan 和 rowspan;
5、
修改控件Margin
属性,此
属性为当前控件与另一控件的边距距离;
此方式可以设置控件随桌面大小自动缩放
要强制立即生效WPF控件属性的更改,可以使用控件的UpdateLayout()方法。这个方法将强制控件重新计算其布局,并立即应用任何更改。例如,如果您更改了控件的宽度属性,然后希望立即使更改生效,可以使用以下代码:
myControl.Width = 100; //更改控件宽度
myControl.UpdateLayout(); //强制立即生效
请注意,UpdateLayout()方法可能会导致性能问题,因为它会导致整个布局重新计算。因此,只有在必要时才应该使用它。