【问题标题】:Adding child controls to a WrapPanel将子控件添加到 WrapPanel
【发布时间】:2013-12-03 11:54:44
【问题描述】:

我对 WPF 非常陌生。

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

我有一个堆栈面板 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>

我的 xaml 结构就是这样。

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

如何做到这一点?

【问题讨论】:

  • “下来”是什么意思?
  • 填充说5个控件后如果没有空间,那么第6个必须进入stackpanel的下一行
  • StackPanel 不应用于实现您描述的行为。您可以尝试 WrapPanel 或编写自己的自定义面板。

标签: c# wpf


【解决方案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" Height="300" Width="300">
    <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)
                });
            }
        }
    }
}

执行结果:

【讨论】:

  • 我们怎样才能一次添加两个控件(文本框+标签)作为一个孩子。在上面的示例中,您添加了 5 次矩形,我想添加 5 次矩形+标签(作为单个子项)
  • @Surya 在 WPF 中,所有控件都可以嵌套。例如,您可以制作另一个堆栈面板,将文本块和标签添加到该堆栈面板,然后将堆栈面板作为单个控件添加到包装面板。这有意义吗?
【解决方案2】:

您应该使用WrapPanel 来满足您的要求。

【讨论】:

    【解决方案3】:

    将您的StackPanel 包裹在ScrollViewer 中,并在您刚刚添加的元素上调用ScrollIntoView()

    【讨论】:

      【解决方案4】:

      而不是使用StackPanel 并添加UserControls 使用ListBox,您将 绑定到ObservableCollection。 'ObservableCollection' 将在添加/删除项目时通知 ListBox。
      将 ListBox.ItemsPanel 更改为 Wrappanel 而不是 StackPanel

      取自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."

      您的 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>
      

      【讨论】:

        【解决方案5】:

        最好在你的情况下使用它有一个附加属性为此准备好,但 StackPanel 将在 FIFO 中工作

        DockPanel

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

        但如果你想在后面的代码中使用它

        spTerminalBox.Children.Add("yourControl"); 
        

        【讨论】:

        • 是的,我做到了。一旦超出堆栈面板宽度,问题就在于控件的可见性。我需要它不要开箱即用。我必须来到下面
        猜你喜欢
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 2013-07-16
        • 1970-01-01
        • 2018-02-24
        • 1970-01-01
        • 2011-10-03
        • 1970-01-01
        相关资源
        最近更新 更多