添加复选框UniformGrid [英] Adding checkBoxes to UniformGrid

查看:177
本文介绍了添加复选框UniformGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态复选框添加到uniformgrid在WPF。
,而它看起来像网格不分配足够的空间,所以他们都还挺奠定了对方。
我这是怎么将它们添加在后面的代码:

I am trying to dynamically add checkboxes to an uniformgrid in wpf. But it looks like the grid doesn't allocate them enough space and so they all kinda lay over each other. This is how I add them in code behind:

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

This is how my XAML looks (I placed the uniformgrid in a scrollviewer)

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



这是它的外观在程序中:

And this is how it looks in the program:

我只是希望每一个复选框有足够的空间,从而使内容可以充分阅读。

I just want that every checkBox has enough space, so that the content can be fully read.

推荐答案

你的添加UI元素动态?你不能只预定义的复选框模板,并添加 CheckBox.Content 呢?如果可能的话再定义一个的ObservableCollection 包含您 CheckBox.Content 是这样的:

do you have to add UI elements dynamically? can't you just predefine your CheckBox template and add CheckBox.Content instead? If it's possible then define an ObservableCollection that contains your CheckBox.Contentlike this:

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



再定义一个的ItemsControl 和绑定的的ItemsSource 到您的收藏:

then define an ItemsControl and bind it's ItemsSource to your 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>



这样一来,所有项目具有相同的宽度,因为它们共享一个尺码组,而且因为他们是大小自动,他们还将向规模最大的 CheckBox.Content

This way, All Items have the same width as they share a size group, moreover because they are sized Auto, they will also size to the largest CheckBox.Content.

这篇关于添加复选框UniformGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆