在WPF中,我有一个布尔到长度转换器,我已将其绑定到列和行定义。
此转换器用于读取绑定的布尔值,以确定是应隐藏还是显示行/列。
这样做是为了让我可以“最大化”网格的给定部分或将其恢复到原始大小。只要我不使用gridsplitter来调整大小,这就可以工作。一旦发生这种情况,它就不再设置所需的长度。我有一种感觉,一旦使用了gridsplitter,它就会删除对列定义的绑定。有解决方法吗?
转换器
[ValueConversion(typeof(bool), typeof(GridLength))]
public class BoolToGridLengthConverter : IValueConverter
{
public int Length { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((bool)value == true) ? new GridLength(Length == 0 ? 1 : Length, GridUnitType.Star) : new GridLength(0);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{ // Don't need any convert back
return null;
}
}
XAML
<Grid>
<Grid.Resources>
<helpers:BoolToGridLengthConverter x:Key="BoolToGridLengthConverter1" Length="1" />
<helpers:BoolToGridLengthConverter x:Key="BoolToGridLengthConverter2" Length="2" />
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding ShowColumn1, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="{Binding ShowColumn2, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="{Binding ShowColumn3, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding ShowRow1, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter1}}" />
<RowDefinition Height="5" />
<RowDefinition Height="{Binding ShowRow2, Mode=TwoWay, Converter={StaticResource BoolToGridLengthConverter2}}" />
</Grid.RowDefinitions>
<Grid Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="0"></Grid>
<GridSplitter Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="1" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="5" />
<Grid Grid.Column="0" Grid.Row="2"></Grid>
<GridSplitter Grid.Column="1" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />
<Grid Grid.Column="2" Grid.Row="2"></Grid>
<GridSplitter Grid.Column="3" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />
<Grid Grid.Column="4" Grid.Row="2"></Grid>
</Grid>
编辑:重组问题以包含更多XAML
编辑:附加信息,当我希望展开网格部分时,我将所有布尔值设置为false并将我想要的部分设置为true。例如,如果我希望最大化Grid.Column 2和Grid.Row 2,我将所有布尔值设置为false,除了ShowColumn2和ShowRow2。这可以按预期工作,当布尔值全部设置为true时,为了获得原始视图,它也可以按预期工作。问题是,一旦我使用gridsplitter调整列的大小,它就像以前一样不起作用。当我最大化相同的部分时,它工作正常,但当我尝试调整大小恢复到原始大小Grid.Column 2和Grid.Row 2占用整个底部行。除了它们之外的两列最小化。
这让我相信,当使用网格探测器时,转换器将无法再将列的值设置为true。
答案 0 :(得分:1)
在ColumnDefinition
中,您需要将MaxWidth
设置为零,以隐藏Column
并将MaxHeight
隐藏为Row
。
尝试以下转换器:
[ValueConversion(typeof(bool), typeof(double))]
public class BoolToMaxConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((bool)value == true) ? double.MaxValue : 0d;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{ // Don't need any convert back
return null;
}
}
和你的xaml:
<Grid>
<Grid.Resources>
<local:BoolToMaxConverter x:Key="BoolToMaxConverter" />
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="{Binding ShowColumn1, Converter={StaticResource BoolToMaxConverter}}" />
<ColumnDefinition Width="5" />
<ColumnDefinition MaxWidth="{Binding ShowColumn2, Converter={StaticResource BoolToMaxConverter}}" />
<ColumnDefinition Width="5" />
<ColumnDefinition MaxWidth="{Binding ShowColumn3, Converter={StaticResource BoolToMaxConverter}}" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition MaxHeight="{Binding ShowRow1, Converter={StaticResource BoolToMaxConverter}}" />
<RowDefinition Height="5" />
<RowDefinition MaxHeight="{Binding ShowRow2, Converter={StaticResource BoolToMaxConverter}}" />
</Grid.RowDefinitions>
<Grid Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="0"></Grid>
<GridSplitter Grid.Column="0" Grid.ColumnSpan="5" Grid.Row="1" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="5" />
<Grid Grid.Column="0" Grid.Row="2"></Grid>
<GridSplitter Grid.Column="1" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />
<Grid Grid.Column="2" Grid.Row="2"></Grid>
<GridSplitter Grid.Column="3" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="5" />
<Grid Grid.Column="4" Grid.Row="2"></Grid>
</Grid>
另请考虑删除TwoWay-Binding
。