c# - 当第二列的内容可见时,如何将 Xaml 网格列 50/50 拆分?

标签 c# xaml windows-runtime grid

对于具有 2 列的网格,只有当第二列的内容可见时,我如何才能将其按 50/50 的宽度拆分?

<Grid>
   <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
   </Grid.ColumnDefinitions>

   <Grid Grid.Column="0">
      <!-- Some content here -->
   </Grid>

   <Grid Grid.Column="1" Visibility="{Binding HasValue, Converter={StaticResource BooleanToVisibilityConverter}}">
      <!-- Some content here -->
   </Grid>    
</Grid>

如果 HasValue 为真,则网格需要按 50/50 拆分,否则,第一列必须占据整个屏幕。

最佳答案

创建一个转换器 BoolToGridLengthConverter 并将其作为静态资源放入您的 App.xaml 中:

/// <summary>
/// Converts a boolean value to a grid length which is specified with a converter
/// parameter if the value is true, otherwise the grid lengthe will be zero.
/// <para>
/// If no parameter is specified the returned <see cref="Windows.UI.Xaml.GridLength"/> will be zero.
/// If the parameter cannot be parsed to a grid length then the returned <see cref="Windows.UI.Xaml.GridLength"/> will be zero.
/// If the value is a <see cref="bool"/> false, then the returned <see cref="Windows.UI.Xaml.GridLength"/> will be zero.
/// </para>
/// </summary>
public sealed class BoolToGridLengthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (!(value is bool) || !(parameter is string))
        {
            return new GridLength(0);
        }

        if (!((bool)value))
        {
            return new GridLength(0);
        }

        var str = parameter as string;
        if (str.Equals("Auto"))
        {
            return new GridLength(0, GridUnitType.Auto);
        }

        if (str.Equals("*"))
        {
            return new GridLength(1, GridUnitType.Star);
        }

        if (str.EndsWith("*"))
        {
            var length = Double.Parse(str.Substring(0, str.Length - 1));
            return new GridLength(length, GridUnitType.Star);
        }

        var len = Double.Parse(str);
        return new GridLength(len);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

然后您可以像这样在您的 xaml 中使用它:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="{Binding HasValue, Converter={StaticResource BoolToGridLengthConverter}, ConverterParameter='*'}"/>
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0">
            <!--  Some content here  -->
        </Grid>

        <Grid Grid.Column="1" Visibility="{Binding HasValue, Converter={StaticResource BooleanToVisibilityConverter}}">
            <!--  Some content here  -->
        </Grid>
    </Grid>

仅当 bool 值为 true 时,转换器才会使用 ConverterParameter,否则列宽将设置为零。转换器参数,可以是Auto,或者例如0.5*或者任何固定宽度例如50.5

关于c# - 当第二列的内容可见时,如何将 Xaml 网格列 50/50 拆分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32278171/

相关文章:

c# - 具有通用数据库上下文的存储库模式

c# - MvvmCross vnext : merge plugins with monodroid

c# - 如何配置 UseSqlServer?

.net - 如何根据默认样式创建样式?

C#:System.Windows.Controls.UserControl 作为列表框

c# - 如何通过 3G 连接检查互联网连接状态?

c# - 运行时尝试查找 exe/dll 而不是 .winmd 引用

c# - 远程桌面连接中的用户 IP

xaml - Expression Blend 会让我免于学习 xaml 吗?

c# - 在 Windows 应用商店应用程序中使用 httpclient 获取 UTF-8 响应