Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 使用拆分器后如何重置网格行高度?_Wpf - Fatal编程技术网

Wpf 使用拆分器后如何重置网格行高度?

Wpf 使用拆分器后如何重置网格行高度?,wpf,Wpf,对于以下XAML,取消选中复选框时,最下面一行将隐藏。在使用gridsplitter调整大小之前,一切都很好。然后,选中/取消选中复选框不起任何作用。如果转换器将高度设置为0,我希望该行隐藏。发生什么事?移动拆分器后如何重置高度 <Grid> <Grid.Resources> <m:CheckedToLengthConverter x:Key="checkedToLengthConverter" /> </Grid.Res

对于以下XAML,取消选中复选框时,最下面一行将隐藏。在使用gridsplitter调整大小之前,一切都很好。然后,选中/取消选中复选框不起任何作用。如果转换器将高度设置为0,我希望该行隐藏。发生什么事?移动拆分器后如何重置高度

<Grid>
    <Grid.Resources>
        <m:CheckedToLengthConverter x:Key="checkedToLengthConverter" />
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="3*" />
        <RowDefinition Height="{Binding Mode=OneWay, ElementName=ShowBottomCheckBox, Path=IsChecked, Converter={StaticResource checkedToLengthConverter}, ConverterParameter=2}" />
    </Grid.RowDefinitions>
    <Border Background="Blue" />
    <CheckBox Name="ShowBottomCheckBox" IsChecked="True" />
    <GridSplitter HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="5" />
    <Border Background="Red" Grid.Row="1" />
</Grid>

问题是,一旦移动拆分器,第一行将具有明确的宽度,因此将最后一行设置回*将不会产生任何效果。经过一些实验,我想出了下面的代码。请注意,您需要指定双向绑定,否则它将无法工作

public class CheckedToLengthConverter : MarkupExtension, IValueConverter
{
    public GridLength TrueValue { get; set; }
    public GridLength FalseValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return System.Convert.ToBoolean(value) ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }

    #region Overrides of MarkupExtension

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    #endregion
}

<Grid>
    <Grid.Resources>
        <m:CheckedToLengthConverter TrueValue="2*" FalseValue="*" x:Key="c1" />
        <m:CheckedToLengthConverter TrueValue="3*" FalseValue="0" x:Key="c2" />
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, 
                       ElementName=ShowBottomCheckBox, 
                       Converter={StaticResource c1}}"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, 
                       ElementName=ShowBottomCheckBox, 
                       Converter={StaticResource c2}}"/>
    </Grid.RowDefinitions>
    <Border Background="Blue" />
    <CheckBox Name="ShowBottomCheckBox" IsChecked="True" />
    <GridSplitter HorizontalAlignment="Stretch" 
                  Grid.Row="1" VerticalAlignment="Bottom" Height="5" 
                  ResizeBehavior="PreviousAndNext" />
    <Border Background="Red" Grid.Row="2" />
</Grid>
public类CheckedToLengthConverter:MarkupExtension,IValueConverter
{
公共GridLength TrueValue{get;set;}
公共GridLength错误值{get;set;}
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回System.Convert.ToBoolean(值)?TrueValue:FalseValue;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
不做任何事;
}
#MarkupExtension的区域覆盖
公共覆盖对象ProviderValue(IServiceProvider服务提供程序)
{
归还这个;
}
#端区
}

这是用户体验问题,而不是代码。你想做的没有什么意义,你试过用视觉状态来代替吗

public class CheckedToLengthConverter : MarkupExtension, IValueConverter
{
    public GridLength TrueValue { get; set; }
    public GridLength FalseValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return System.Convert.ToBoolean(value) ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }

    #region Overrides of MarkupExtension

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    #endregion
}

<Grid>
    <Grid.Resources>
        <m:CheckedToLengthConverter TrueValue="2*" FalseValue="*" x:Key="c1" />
        <m:CheckedToLengthConverter TrueValue="3*" FalseValue="0" x:Key="c2" />
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, 
                       ElementName=ShowBottomCheckBox, 
                       Converter={StaticResource c1}}"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="{Binding Mode=TwoWay, Path=IsChecked, 
                       ElementName=ShowBottomCheckBox, 
                       Converter={StaticResource c2}}"/>
    </Grid.RowDefinitions>
    <Border Background="Blue" />
    <CheckBox Name="ShowBottomCheckBox" IsChecked="True" />
    <GridSplitter HorizontalAlignment="Stretch" 
                  Grid.Row="1" VerticalAlignment="Bottom" Height="5" 
                  ResizeBehavior="PreviousAndNext" />
    <Border Background="Red" Grid.Row="2" />
</Grid>