我想在 wpf 中制作一个 uniformGrid。
当我有 1 个孩子时,我希望它填充网格,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>
我在这篇文章中发现我必须制作一个自定义的 UniformGrid 以便网格的 Children.count 真正起作用,所以我这样做了。
这段代码一切正常,但我还有一个问题。
这些按钮可以动态显示或折叠,当按钮折叠时,我不希望它们与行数无关。当按钮 3 和 4 折叠时,我不想有第二行。
知道如何实现这一目标吗?