将子控件添加到WrapPanel

 曾那么执着o的厮守 发布于 2023-02-13 11:18

我是WPF的新手.

我有一个非常简单的问题.

我有一个stackpanel spTerminalBox.


    
        
        
        
    
    
        
        
    
    
    

我的xaml结构就是这样.

我在代码中动态填充该堆栈面板中的用户控件.一旦StackPanel上的子元素不适合StackPanel区域,那么它不应该超出可见区域,它应该下降.

怎么做到这一点?

1 个回答
  • XAML:

    <Window x:Class="WpfTestBench.PanelSample"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="PanelSample"  >
        <Grid>
            <WrapPanel Name="MyPanel" />
        </Grid>
    </Window>
    

    代码隐藏:

    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Shapes;
    
    namespace WpfTestBench
    {
        public partial class PanelSample
        {
            public PanelSample()
            {
                InitializeComponent();
    
                for (var i = 0; i < 5; i++)
                {
                    MyPanel.Children.Add(new Rectangle
                    {
                        Width = 100,
                        Height = 20,
                        StrokeThickness = 1,
                        Stroke = new SolidColorBrush(Colors.Black),
                        Margin = new Thickness(5)
                    });
                }
            }
        }
    }
    

    执行结果:

    宽 狭窄

    2023-02-13 11:22 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有