【问题标题】:wpf data binding grid row visibilitywpf 数据绑定网格行可见性
【发布时间】:2011-06-14 17:23:22
【问题描述】:

我有一个带有网格的窗口,其作用类似于表单。该窗口不是我自己的,并且有一个新要求,即根据用户选择的上下文不显示(即折叠)第 4 行和第 5 行。

要完成这项工作,我能想到的两件事是:

  1. 在行内容上有一个转换器,该转换器采用布尔值,如果为真则折叠可见性。
  2. 在网格行高属性上有一个转换器。

我更喜欢后者,但无法获得转换器的输入值。转换器代码和绑定如下。

谁能告诉我绑定应该是什么样子才能完成这项工作?有没有更简单的方法来做到这一点?

转换器代码

[ValueConversion(typeof(GridLength), typeof(Visibility))]
public class GridLengthToCollapseVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        if (value == null || parameter == null) return Binding.DoNothing;

        var result = (GridLength) value;
        bool shouldCollapse;
        Boolean.TryParse(parameter.ToString(), out shouldCollapse);
        return shouldCollapse ? new GridLength() : result;

    }

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

绑定(这是我卡住的地方)

假设我希望高度的值为 30,除非绑定的 ShowLastName 属性为真。 下面的绑定不正确,那是什么?

 <RowDefinition Height="{Binding Source=30, Converter={StaticResource GridLengthToCollapseVisibilityConv},ConverterParameter=ShowLastName}" />

工作解决方案

[ValueConversion(typeof(bool), typeof(GridLength))]
public class GridLengthToCollapseVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        if (value == null || parameter == null) return Binding.DoNothing;

        bool shouldCollapse;
        Boolean.TryParse(value.ToString(), out shouldCollapse);
        return shouldCollapse 
            ? new GridLength(0) 
            : (GridLength) parameter;
    }

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

}

    <Grid.Resources>
        <cvt:GridLengthToCollapseVisibilityConverter x:Key="GridLengthToCollapseVisibilityConv" />
        <GridLength x:Key="AutoSize">Auto</GridLength>
        <GridLength x:Key="ErrorLineSize">30</GridLength>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="{StaticResource AutoSize}" />
        <RowDefinition Height="{StaticResource ErrorLineSize}" />
        <RowDefinition Height="{Binding Path=HideLastName, 
            Converter={StaticResource GridLengthToCollapseVisibilityConv},ConverterParameter={StaticResource AutoSize}}" />
        <RowDefinition Height="{Binding Path=HideLastName, 
            Converter={StaticResource GridLengthToCollapseVisibilityConv},ConverterParameter={StaticResource ErrorLineSize}}" />
    </Grid.RowDefinitions>

【问题讨论】:

  • 今天从您的帖子中学到了一些新东西... ValueConversion 属性和 Binding.DoNothing。谢谢。

标签: wpf data-binding forms visibility


【解决方案1】:

您无法对 ConverterParamater 进行数据绑定:http://social.msdn.microsoft.com/Forums/en/wpf/thread/88a22766-5e6f-4a16-98a6-1ab39877dd09

如果高度始终相同,为什么不切换值和参数:

<RowDefinition Height="{Binding Source=ShowLastName, Converter={StaticResource GridLengthToCollapseVisibilityConv},ConverterParameter=30}" />

如果您需要对两个值进行数据绑定,您可以使用多值绑定:http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx

【讨论】:

  • 您好,感谢您提供切换值和参数的想法,这使其更易于实现。只要绑定到 DP,就可以绑定 ConverterParameter(我以前做过)。干杯!
【解决方案2】:

你所要做的就是交换绑定和参数。


如果您仍然希望这两个值都是数据绑定的,请使用 MultiBinding,即使您的第二个值是常量。这是一种 hack,但它是向转换器传递额外值的最简单方法。

【讨论】:

  • btw - 参数不能绑定,因为它不是 DP,不是机会。标记扩展有一些技巧,但我不会走这条路。
猜你喜欢
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
相关资源
最近更新 更多