调整均匀网格到孩子的数量

时间:2013-10-01 09:52:53

标签: c# wpf uniformgrid

我想在wpf中创建一个uniformGrid。

当我有一个孩子时,我希望它填满网格,2个孩子我想要2列,当我有3或4个孩子时,我想要2行2列。

所以我制作了一个转换器,用于检查网格中有多少个子节点,并根据它确定行数/列数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows;
using System.Globalization;

namespace UserControlSolution.Converter
{
    [ValueConversion(typeof(int), typeof(int))]
    public class CountToDimensionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int itemsCount = (int)value;
            int dimensionLength = 1;
            if (itemsCount > 0)
            {   
                if (string.Equals((string)parameter, "Rows", StringComparison.OrdinalIgnoreCase))
                {
                    switch(itemsCount)
                    {
                        case 1: 
                        case 2: dimensionLength = 1;
                            break;
                        case 3: 
                        case 4: dimensionLength = 2;
                            break;
                    }
                }

                if (string.Equals((string)parameter, "Columns", StringComparison.OrdinalIgnoreCase))
                {
                    switch(itemsCount)
                    {
                        case 1: dimensionLength = 1;
                            break;
                        case 2: 
                        case 3: 
                        case 4: dimensionLength = 2;
                            break;
                    }
                }
            }

            return dimensionLength;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}

这是我的xaml:

<customGridView:MyUniformGrid x:Name="AlarmButtonGrid" Margin="0,10" Grid.Row="2" Width="{Binding ActualWidth, ElementName=AlarmPictureBox}"
        Rows="{Binding RelativeSource={RelativeSource Self}, Path=Children.Count, Converter={StaticResource CountToDimensionConverter}, ConverterParameter=Rows}" 
        Columns="{Binding RelativeSource={RelativeSource Self}, Path=Children.Count, Converter={StaticResource CountToDimensionConverter}, ConverterParameter=Columns}"> 

       <Button x:Name="Button1" Content="Sluit" Height="35" Style="{StaticResource CustomButtonStyle}" Margin="5" />
       <Button x:Name="Button2" Content="Verbergen" Height="35" Style="{StaticResource CustomButtonStyle}" Margin="5"/>
       <Button x:Name="Button3" Visibility="Collapsed" Content="Extra rij" Height="35" Style="{StaticResource CustomButtonStyle}" Margin="5"/>
       <Button x:Name="Button4" Visibility="Collapsed" Content="Extra rij" Height="35" Style="{StaticResource CustomButtonStyle}" Margin="5"/>
</customGridView:MyUniformGrid>

Binding to Children.Count

我在这篇文章中发现我必须制作一个自定义的UniformGrid,以便网格的Children.count实际工作,所以我就这样做了。

此代码全部有效,但我还有一个问题。

这些按钮可以动态制作可见或折叠,当按钮折叠时,我不希望它们对行数有影响。 按钮3&amp; 4折叠我不想有第2行。

知道怎么做到这一点?

2 个答案:

答案 0 :(得分:2)

MyUniformGrid课程中,只需添加新媒体资源:

public int VisibleCount
{
    get { return Children.OfType<UIElement>().Count(c => c.Visibility == 
              Visibility.Visible); }
}

...而是绑定到那个:

<customGridView:MyUniformGrid x:Name="AlarmButtonGrid" Margin="0,10" Grid.Row="2" 
    Width="{Binding ActualWidth, ElementName=AlarmPictureBox}" Rows="{Binding 
    RelativeSource={RelativeSource Self}, Path=VisibleCount, Converter={
    StaticResource CountToDimensionConverter}, ConverterParameter=Rows}" Columns="{
    Binding RelativeSource={RelativeSource Self}, Path=VisibleCount, Converter={
    StaticResource CountToDimensionConverter}, ConverterParameter=Columns}">                    
    <Button x:Name="Button1" Content="Sluit" Height="35" Style="{StaticResource CustomButtonStyle}" Margin="5" />
    <Button x:Name="Button2" Content="Verbergen" Height="35" Style="{StaticResource CustomButtonStyle}" Margin="5"/>
    <Button x:Name="Button3" Visibility="Collapsed" Content="Extra rij" Height="35" Style="{StaticResource CustomButtonStyle}" Margin="5"/>
    <Button x:Name="Button4" Visibility="Collapsed" Content="Extra rij" Height="35" Style="{StaticResource CustomButtonStyle}" Margin="5"/>
</customGridView:MyUniformGrid>

唯一需要注意的是,无论何时在INotifyPropertyChanged.PropertyChanged集合中添加或删除项目,您都可能需要调用Grid.Children事件...或者,您可以将其实现为{{1相反。

答案 1 :(得分:1)

这样做的一种方法是在uniformgrid的Add / Remove方法中的UIElement上添加/删除IsVisibleChanged的侦听器。

并将逻辑放在处理程序中以计算依赖于Visible子项的行/列。然后你不需要行/列和转换器的自绑定。

即。在添加方法

   element.IsVisibleChanged += new DependencyPropertyChangedEventHandler(Child_IsVisibleChanged);

然后在处理程序

    private void Child_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        int visibleChildrenCount = 0;
        for (int i = 0; i < grid.Children.Count; i++)
        {
            if (grid.Children[i].Visibility == Visibility.Visible)
            {
                visibleChildrenCount++;
            }
        }

        //Here you can set your Rows and Columns depending on visiblechildrenCount
    }

您可以创建单独的方法来执行此操作,并从Add元素调用该方法并删除元素方法。 不要忘记在删除方法

中删除元素时删除处理程序