将复选框添加到 UniformGrid

Adding checkBoxes to UniformGrid

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

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都有足够的space,这样内容才能完整阅读。

我建议使用 WrapPanel

Then how can I do it, that each cell has the size of the checkBox with the biggest content?

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

UniformGridRowsColumns 属性 都设置为零时,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;
        }
    }
}

必须动态添加UI元素吗?您不能只预定义 CheckBox 模板并添加 CheckBox.Content 吗?如果可能,请定义一个包含您的 CheckBox.ContentObservableCollection,如下所示:

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

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

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

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