自定义StackPanel模板,实现自定义顺序排列

自定义StackPanel模板

实现从左到右,从上到下,从下到上,从右到左排列
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace MyStackPanelLib
{
    public class MyStackPanelCtrl : Panel
    {
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(MyStackPanelCtrl), new FrameworkPropertyMetadata(Orientation.LeftToRight, FrameworkPropertyMetadataOptions.AffectsArrange));

        /// <summary>
        /// 获取或设置控件中子控件的布局方式
        /// </summary>
        [System.ComponentModel.Category("Layout")]
        public Orientation Orientation
        {
            get
            {
                return (Orientation)this.GetValue(OrientationProperty);
            }
            set
            {
                this.SetValue(OrientationProperty, value);
            }
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            Size childrenSize = new Size(0, 0);

            foreach (UIElement child in this.Children)
            {
                child.Measure(new Size(Double.PositiveInfinity,Double.PositiveInfinity));
                childrenSize.Width += child.DesiredSize.Width;
                childrenSize.Height += child.DesiredSize.Height;
            }

            return childrenSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            switch (this.Orientation)
            {
                case Orientation.LeftToRight:
                    {
                        Point childPos = new Point(0, 0);
                        foreach (UIElement child in this.Children)
                        {
                            child.Arrange(new Rect(childPos, new Size(child.DesiredSize.Width, finalSize.Height)));
                            childPos.X += child.RenderSize.Width;
                        }
                    }
                    break;
                case Orientation.RightToLeft:
                    {

                        Point childPos = new Point(0, 0);

                        //此IF语句块的目的在于,对第一个被摆放的孩子调用Arrange方法,以便得到其RenderSize,
                        //这样就可以用控件的finalSize.Width - firstChild.RenderSize.Width得到第一个被摆放的
                        //孩子应该被放置的位置的X坐标
                        if (this.Children.Count > 0)
                        {
                            UIElement firstChild = this.Children[this.Children.Count - 1];
                            firstChild.Arrange(new Rect(childPos, new Size(firstChild.DesiredSize.Width, finalSize.Height)));
                            childPos.X = finalSize.Width - firstChild.RenderSize.Width;
                        }

                        foreach (UIElement child in this.Children)
                        {
                            child.Arrange(new Rect(childPos, new Size(child.DesiredSize.Width, finalSize.Height)));
                            childPos.X -= child.RenderSize.Width;
                        }
                    }
                    break;
                case Orientation.TopToBottom:
                    {
                        Point childPos = new Point(0, 0);
                        foreach (UIElement child in this.Children)
                        {
                            child.Arrange(new Rect(childPos, new Size(finalSize.Width,child.DesiredSize.Height)));
                            childPos.Y += child.RenderSize.Height;
                        }
                    }
                    break;
                case Orientation.BottomToTop:
                    {
                        Point childPos = new Point(0, 0);

                        //此IF语句块的目的在于,对第一个被摆放的孩子调用Arrange方法,以便得到其RenderSize,
                        //这样就可以用控件的finalSize.Height - firstChild.RenderSize.Height得到第一个被摆放的
                        //孩子应该被放置的位置的Y坐标
                        if (this.Children.Count > 0)
                        {
                            UIElement firstChild = this.Children[this.Children.Count - 1];
                            firstChild.Arrange(new Rect(childPos, new Size(finalSize.Width, firstChild.DesiredSize.Height)));
                            childPos.Y = finalSize.Height - firstChild.RenderSize.Height;
                        }

                        foreach (UIElement child in this.Children)
                        {
                            child.Arrange(new Rect(childPos, new Size(finalSize.Width, child.DesiredSize.Height)));
                            childPos.Y -= child.RenderSize.Height;
                        }
                       
                    }
                    break;
                default:
                    break;
            }


            return finalSize;
        }
    
    }

    public enum Orientation
    {
        LeftToRight,
        RightToLeft,
        TopToBottom,
        BottomToTop
    }
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用ToggleButton和StackPanel等控件结合DataTemplate实现定义导航栏的例子: 1. 在WPF窗口中添加一个StackPanel控件: ```xml <StackPanel Orientation="Horizontal" VerticalAlignment="Top"> <!-- 这里将会添加ToggleButton控件 --> </StackPanel> ``` 2. 在StackPanel中添加多个ToggleButton控件,每个ToggleButton代表一个导航项,并使用DataTemplate定义ToggleButton的显示内容: ```xml <StackPanel Orientation="Horizontal" VerticalAlignment="Top"> <ToggleButton x:Name="btnPage1" ContentTemplate="{StaticResource NavigationButtonTemplate}" Command="{Binding NavigateToPage1Command}" IsChecked="{Binding IsPage1Selected}"/> <ToggleButton x:Name="btnPage2" ContentTemplate="{StaticResource NavigationButtonTemplate}" Command="{Binding NavigateToPage2Command}" IsChecked="{Binding IsPage2Selected}"/> <ToggleButton x:Name="btnPage3" ContentTemplate="{StaticResource NavigationButtonTemplate}" Command="{Binding NavigateToPage3Command}" IsChecked="{Binding IsPage3Selected}"/> </StackPanel> ``` 其中,ContentTemplate使用了一个名为NavigationButtonTemplate的DataTemplate,它可以在Resources中定义,用于定义ToggleButton的显示内容。 3. 在Resources中定义NavigationButtonTemplate: ```xml <Window.Resources> <DataTemplate x:Key="NavigationButtonTemplate"> <StackPanel Orientation="Horizontal"> <Image Source="{Binding Icon}" Width="16" Height="16"/> <TextBlock Text="{Binding Text}" Margin="5,0,0,0"/> </StackPanel> </DataTemplate> </Window.Resources> ``` 其中,Image和TextBlock用于显示导航项的图标和文本,Icon和Text可以在ViewModel中绑定。 4. 在ViewModel中定义IsPage1Selected、IsPage2Selected、IsPage3Selected属性和NavigateToPage1Command、NavigateToPage2Command、NavigateToPage3Command命令,用于处理导航项的选择和导航操作。 这样,一个使用ToggleButton和StackPanel等控件结合DataTemplate实现定义导航栏的例子就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值