WPF 自适应布局控件

    public class KDLayoutGroup : Grid
    {
        public double LabelWidth { get; set; }


        public double GetLabelWidth()
        {
            return LabelWidth;
        }

        public void SetLabelWidth(double value)
        {
            if (this.Parent is KDLayoutControl)
            {
                double w = (this.Parent as KDLayoutControl).GetLableWidth();
                if (w < value)
                {
                    (this.Parent as KDLayoutControl).SetLabelWidth(value);
                }

                for (int i = 0; i < Children.Count; i++)
                {

                    SetBatchLabelWidth(Children[i], value);
                }

            }
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            if (Children.Count == this.ColumnDefinitions.Count)
                return;

            for (int i = 0; i < Children.Count; i++)
            {
                var column = new ColumnDefinition();
                //column.Width = new GridLength(0,GridUnitType.Auto);
                this.ColumnDefinitions.Add(column);

                Children[i].SetValue(Grid.ColumnProperty, i);

                SetBatchLabelWidthOther(Children[i]);

            }

            base.OnRenderSizeChanged(sizeInfo);
        }

        private void SetBatchLabelWidth(UIElement el, double value)
        {
            if (el is KDLayoutItem)
            {
                double width = (el as KDLayoutItem).GetLabelWidht();
                if (width < value)
                {
                    (el as KDLayoutItem).SetLabelWidht(value);
                }
            }
            else
            {
                if (el is Panel)
                {
                    var cs = (el as Panel).Children;
                    for (int i = 0; i < cs.Count; i++)
                    {
                        SetBatchLabelWidth(cs[i], value);
                    }
                }

            }
        }

        private void SetBatchLabelWidthOther(UIElement el)
        {
            if (el is KDLayoutItem)
            {

                double width = (el as KDLayoutItem).GetLabelWidht();
                if (width > LabelWidth)
                {
                    LabelWidth = width;
                    SetLabelWidth(width);
                }
            }
            else
            {
                if (el is Panel)
                {
                    var cs = (el as Panel).Children;
                    for (int i = 0; i < cs.Count; i++)
                    {
                        SetBatchLabelWidthOther(cs[i]);
                    }
                }

            }
        }
    }
 public class KDLayoutControl : StackPanel
    {

        public double LabelWidth { get; set; }
        public double GetLableWidth()
        {
            return LabelWidth;
        }

        public void SetLabelWidth(double value)
        {
            LabelWidth = value;

            for (int i = 0; i < Children.Count; i++)
            {
                if ((Children[i] as KDLayoutGroup).GetLabelWidth() < LabelWidth)
                {
                    (Children[i] as KDLayoutGroup).SetLabelWidth(LabelWidth);
                }
            }
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            for (int i = 0; i < Children.Count; i++)
            {

                if ((Children[i] as KDLayoutGroup).GetLabelWidth() > LabelWidth)
                {
                    LabelWidth = (Children[i] as KDLayoutGroup).GetLabelWidth();
                }
            }

            SetLabelWidth(LabelWidth);

            base.OnRenderSizeChanged(sizeInfo);
        }
    }
<UserControl x:Class="PHMES.UI.Base.KDLayoutItem"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:PHMES.UI.Base"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Template>
        <ControlTemplate TargetType="local:KDLayoutItem">
            <DockPanel>
                <Label  x:Name="lbl" VerticalAlignment="Center"  VerticalContentAlignment="Center" Content="{TemplateBinding Label}"  />
                <ContentPresenter/>
            </DockPanel>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>
 public partial class KDLayoutItem : UserControl
    {
        public KDLayoutItem()
        {
            InitializeComponent();
        }
        public object Label
        {
            get { return (object)GetValue(LabelProperty); }
            set
            {
                SetValue(LabelProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Label.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(object), typeof(KDLayoutItem), new PropertyMetadata(null));

 
        public double GetLabelWidht()
        {
            return (this.Template.FindName("lbl",this) as Label).ActualWidth;
        }
        public void SetLabelWidht(double width)
        {
            (this.Template.FindName("lbl", this) as Label).SetValue(WidthProperty,width);
        }
    }

功能类似dev的layoutcontrol,layoutgroup,layoutitem.

用法如下:

<Window x:Class="AutoWidthTest.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:AutoWidthTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <local:KDLayoutControl>
        <local:KDLayoutGroup Height="50" VerticalAlignment="Top"  Margin="3">
            <local:KDLayoutItem Label="aaa">
                <TextBox />
            </local:KDLayoutItem>
            <local:KDLayoutItem Label="bbb">
                <TextBox />
            </local:KDLayoutItem>
            <local:KDLayoutItem Label="ccc">
                <TextBox />
            </local:KDLayoutItem>
        </local:KDLayoutGroup>
        <local:KDLayoutGroup Height="50" VerticalAlignment="Top"  Margin="3">
            <local:KDLayoutItem Label="number">
                <TextBox />
            </local:KDLayoutItem>
            <local:KDLayoutItem Label="name">
                <TextBox />
            </local:KDLayoutItem>
            <local:KDLayoutItem Label="age">
                <TextBox />
            </local:KDLayoutItem>
        </local:KDLayoutGroup>
        <local:KDLayoutGroup Height="50" VerticalAlignment="Top" Margin="3">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <local:KDLayoutItem Label="a">
                    <TextBox />
                </local:KDLayoutItem>
                <local:KDLayoutItem Label="b" Grid.Column="1" Grid.ColumnSpan="2">
                    <TextBox />
                </local:KDLayoutItem>
            </Grid>
        </local:KDLayoutGroup>
    </local:KDLayoutControl>
</Window>

不管label字段有多长,KDLayoutControl会设置容器内所有的label长度一致。

转载于:https://www.cnblogs.com/czly/p/11121916.html

在 C# WPF 中实现页面的自适应分辨率,可以采取以下几个步骤: 1. 使用相对布局:使用 XAML 中的布局容器(如 Grid、StackPanel、WrapPanel 等),并设置控件的行、列以及其他布局属性,以实现自适应布局。 2. 使用 Grid 中的行和列定义:通过设置 Grid 中的行和列的定义,可以让控件根据窗口大小自动调整位置和大小。可以使用 "*" 表示自动调整大小的列或行,使用具体数值(如 "200")表示固定大小的列或行。 3. 使用 ViewBox 控件:ViewBox 是一个用于缩放其内容的容器控件。将需要自适应分辨率的内容放置在 ViewBox 中,并设置 Stretch 属性为 Uniform 或 UniformToFill,即可实现内容的自动缩放。 4. 响应窗口大小改变事件:在窗口的 SizeChanged 事件中编写代码,根据窗口的大小变化,调整控件的位置和大小来实现自适应分辨率。 5. 使用 VisualStateManager:通过使用 VisualStateManager,可以在不同的状态下应用不同的布局。根据窗口大小或其他条件,设置不同的视觉状态,在不同的状态下使用不同的布局。 6. 使用分辨率相关信息:可以通过 System.Windows.Forms.Screen 类获取当前屏幕的分辨率信息,并根据该信息调整控件的布局和大小。 综上所述,以上是一些常用的方法来实现 C# WPF 页面的自适应分辨率。根据具体的需求和场景,可以选择适合的方法来实现页面的自适应
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值