3

我正在尝试将复选框动态添加到 wpf 中的统一网格。但看起来网格没有为它们分配足够的空间,所以它们都有点重叠。这就是我在后面的代码中添加它们的方式:

foreach (string folder in subfolders)
{
  PathCheckBox chk = new PathCheckBox();
  chk.Content = new DirectoryInfo(folder).Name;
  chk.FullPath = folder;
  chk.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
  chk.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;        

  unfiformGridSubfolders.Children.Add(chk);
}

这就是我的 XAML 的外观(我将 uniformgrid 放在滚动查看器中)

<ScrollViewer Grid.Column="1" Grid.RowSpan="3">
  <UniformGrid x:Name="unfiformGridSubfolders" Grid.Column="1" Grid.Row="0" Grid.RowSpan="3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</ScrollViewer>  

这就是它在程序中的样子:

在此处输入图像描述

我只是希望每个checkBox都有足够的空间,这样内容才能被完整阅读。

4

3 回答 3

4

是否必须动态添加 UI 元素?你不能只预定义你的CheckBox模板并添加CheckBox.Content吗?如果可能的话,那么定义一个ObservableCollection包含你CheckBox.Content这样的:

public ObservableCollection<string> SubfolderNames { get; set; }

然后定义一个ItemsControl并将其绑定ItemsSource到您的集合:

    <ItemsControl Grid.Row="0" x:Name="gridSubfolders" ItemsSource="{Binding SubfolderNames}" Grid.IsSharedSizeScope="True">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel></WrapPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="50" />
                    </Grid.ColumnDefinitions>
                    <Border Margin="5" BorderThickness="1" BorderBrush="Black">
                        <CheckBox Content="{Binding}"/>
                    </Border>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

这样,所有项目都具有相同的宽度,因为它们共享一个尺寸组,而且因为它们是 sized Auto,它们也会被设置为最大的CheckBox.Content

于 2015-05-31T13:48:03.227 回答
2

我建议使用类似的东西WrapPanel

那我该怎么做呢,每个单元格都有最大内容的复选框的大小?

使用 UniformGrid 您必须手动检查每个复选框,检查其大小,并将 Uniform Grid.Columns 修改为类似Math.Floor(Grid.CurrentWidth / CheckBoxMaxWidth)

于 2015-05-31T12:59:12.987 回答
0

当 a 的RowsColumns属性UniformGrid都设置为零时,UniformGrid尝试将元素布局在正方形中,而不考虑元素的大小。我会编写一个面板,按照您的需要布局您的元素,如下所示。只需在您的 XAML 中使用MyPanel而不是。UniformGrid

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace MyNamespace
{
    class MyPanel : Panel
    {
        int columns;
        int rows;

        protected override Size MeasureOverride (Size constraint)
        {
            Size childConstraint = constraint;
            double maxChildDesiredWidth = 0.0;
            double maxChildDesiredHeight = 0.0;

            if (InternalChildren.Count > 0)
            {
                for (int i = 0, count = InternalChildren.Count; i < count; ++i)
                {
                    UIElement child = InternalChildren[i];

                    // Measure the child.
                    child.Measure (childConstraint);
                    Size childDesiredSize = child.DesiredSize;

                    if (maxChildDesiredWidth < childDesiredSize.Width)
                    {
                        maxChildDesiredWidth = childDesiredSize.Width;
                    }

                    if (maxChildDesiredHeight < childDesiredSize.Height)
                    {
                        maxChildDesiredHeight = childDesiredSize.Height;
                    }
                }

                columns = (int)(constraint.Width / maxChildDesiredWidth);
                rows = (int)(InternalChildren.Count / columns + 0.5);
            }
            else
            {
                columns = 0;
                rows = 0;
            }

            return new Size ((maxChildDesiredWidth * columns), (maxChildDesiredHeight * rows));
        }

        protected override Size ArrangeOverride (Size arrangeSize)
        {
            Rect childBounds = new Rect (0, 0, arrangeSize.Width / columns, arrangeSize.Height / rows);
            double xStep = childBounds.Width;
            double xBound = arrangeSize.Width - 1.0;

            childBounds.X += 0;

            foreach (UIElement child in InternalChildren)
            {
                child.Arrange (childBounds);

                if (child.Visibility != Visibility.Collapsed)
                {
                    childBounds.X += xStep;
                    if (childBounds.X >= xBound)
                    {
                        childBounds.Y += childBounds.Height;
                        childBounds.X = 0;
                    }
                }
            }

            return arrangeSize;
        }
    }
}
于 2015-05-31T13:41:40.547 回答