Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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
C# 有条件地设置网格列宽度_C#_Wpf_Xaml - Fatal编程技术网

C# 有条件地设置网格列宽度

C# 有条件地设置网格列宽度,c#,wpf,xaml,C#,Wpf,Xaml,我有一个两列的网格。我希望最左边的列的宽度是网格可用宽度的50%,或者是适合列中包含的任何控件所需的宽度,以较大者为准。我怎样才能做到这一点 换言之,我希望列的宽度尽可能小,但至少为50%。如果父列的宽度是先验的,那么这就很容易了。如果父对象的宽度为100,则: <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" MinWidth="50" /> <Col

我有一个两列的网格。我希望最左边的列的宽度是网格可用宽度的50%,或者是适合列中包含的任何控件所需的宽度,以较大者为准。我怎样才能做到这一点


换言之,我希望列的宽度尽可能小,但至少为50%。

如果父列的宽度是先验的,那么这就很容易了。如果父对象的宽度为100,则:

<Grid>
   <Grid.ColumnDefinitions>
     <ColumnDefinition Width="Auto" MinWidth="50" />
     <ColumnDefinition Width="1*" />
   </Grid.ColumnDefinitions>
</Grid>

如果父对象的宽度是先验的,那么这很容易。如果父对象的宽度为100,则:

<Grid>
   <Grid.ColumnDefinitions>
     <ColumnDefinition Width="Auto" MinWidth="50" />
     <ColumnDefinition Width="1*" />
   </Grid.ColumnDefinitions>
</Grid>

理想情况下,您可以将
列定义
上的
MinWidth
属性设置为0.5*。但是,此属性需要一个
Double
,而不是
GridLength
。要解决此问题,您可以使用转换器采用蛮力方法:

XAML

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" MinWidth="{Binding ElementName=otherColumn, Path=ActualWidth, Converter={l:DivideByTwoConverter}}" />
        <ColumnDefinition x:Name="otherColumn" />
    </Grid.ColumnDefinitions>
    <TextBox Grid.Column="0" />
    <TextBox Grid.Column="1" />
</Grid>
public class DivideByTwoConverter : MarkupExtension, IValueConverter
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is double)
        {
            return (double) value/2;
        }

        return DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
在本例中,列以宽度的一半开始。当您在
文本框中输入文本时,它会增长,列也会增长


希望这有帮助

理想情况下,您可以将
列定义
上的
MinWidth
属性设置为0.5*。但是,此属性需要一个
Double
,而不是
GridLength
。要解决此问题,您可以使用转换器采用蛮力方法:

XAML

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" MinWidth="{Binding ElementName=otherColumn, Path=ActualWidth, Converter={l:DivideByTwoConverter}}" />
        <ColumnDefinition x:Name="otherColumn" />
    </Grid.ColumnDefinitions>
    <TextBox Grid.Column="0" />
    <TextBox Grid.Column="1" />
</Grid>
public class DivideByTwoConverter : MarkupExtension, IValueConverter
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is double)
        {
            return (double) value/2;
        }

        return DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
在本例中,列以宽度的一半开始。当您在
文本框中输入文本时,它会增长,列也会增长

希望这有帮助