简体   繁体   English

将子控件添加到WrapPanel

[英]Adding child controls to a WrapPanel

I am very much new to WPF. 我是WPF的新手。

I have a very simple problem. 我有一个非常简单的问题。

I have a stackpanel spTerminalBox. 我有一个stackpanel spTerminalBox。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300"/>
        <ColumnDefinition Width="881*"/>
        <ColumnDefinition Width="11*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="156"/>
        <RowDefinition Height="371*"/>
    </Grid.RowDefinitions>
    <my:WindowHeader x:Name="title" Title="Internet Cafe management software (ICM)"   CloseClicked="window_CloseClicked"   VerticalAlignment="Top" Margin="0,-1,0,0" Grid.ColumnSpan="3" />
    <StackPanel Name ="spTerminalBox" Grid.Column="1" Grid.Row="1" Orientation="Horizontal" Margin="10,10,10,20"/>
</Grid>

My xaml structure is that. 我的xaml结构就是这样。

I am filling a user control in that stackpanel dynamically in code. 我在代码中动态填充该堆栈面板中的用户控件。 Once if child elements on a StackPanel do not fit in the StackPanel area, then it should not go outside of the visible area, it should come down. 一旦StackPanel上的子元素不适合StackPanel区域,那么它不应该超出可见区域,它应该下降。

How to achieve this ? 怎么做到这一点?

XAML: 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" Height="300" Width="300">
    <Grid>
        <WrapPanel Name="MyPanel" />
    </Grid>
</Window>

Codebehind: 代码隐藏:

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)
                });
            }
        }
    }
}

Execution result: 执行结果:

宽狭窄

你应该使用WrapPanel满足你的要求。

StackPanel包装在ScrollViewer并在刚刚添加的元素上调用ScrollIntoView()

Instead of using StackPanel and adding UserControls use ListBox which you'll bind to an ObservableCollection . 而不是使用StackPanel并添加UserControls使用ListBox ,您将绑定到ObservableCollection The 'ObservableCollection' will notify the ListBox when items are added/removed. 添加/删除项目时,'ObservableCollection'将通知ListBox。
Change the ListBox.ItemsPanel to be Wrappanel instead of StackPanel 将ListBox.ItemsPanel更改为Wrappanel而不是StackPanel

taken from http://wpftutorial.net/ 摘自http://wpftutorial.net/
"The wrap panel is similar to the StackPanel but it does not just stack all child elements to one row, it wraps them to new lines if no space is left. The Orientation can be set to Horizontal or Vertical."

you Xaml should look like this: 你Xaml应该是这样的:

<ListBox  x:Name="MyListBox"
          ItemsSource="{Binding Source={StaticResource UserControlsObservableCollection}}"                                  
          HorizontalContentAlignment="Stretch"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
          ScrollViewer.VerticalScrollBarVisibility="Disabled" >
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
              <WrapPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

it will be better to use for your case it has an attached property ready for this but StackPanel will work in FIFO 最好是为你的情况使用它有一个附加的属性准备好,但StackPanel将在FIFO中工作

DockPanel DockPanel中

for example 

            Button btn = new Button();
            DockPanel.SetDock(btn,Dock.Bottom);
            dockPanelTerminalBox.Children.Add(btn); 

but if you want just use this in your code behind 但如果你想在你的代码中使用它

spTerminalBox.Children.Add("yourControl"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM