如何通过绑定在GridViewColumn上设置width=auto



我需要根据布尔值将GridViewColumn的宽度设置为值或auto。我目前有这样的设置:

<GridViewColumn Width="{Binding ColumnWidth}">
public string DepotAssignmentWidth { get { return (hasBoolean) ? "50" : "auto"; } }

这具有所需的行为,但会给我解析错误:

System.Windows.Data Error: 6 : 'SystemConvertConverter' converter failed to convert value 'auto' (type 'String'); fallback value will be used, if available. BindingExpression:Path=Removed; DataItem='Removed' (HashCode=15576908); target element is 'GridViewColumn' (HashCode=46088874); target property is 'Width' (type 'Double') FormatException:'System.FormatException: Input string was not in a correct format.
   at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.String.System.IConvertible.ToDouble(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at MS.Internal.Data.SystemConvertConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'

我也试过这个,它也不起作用:

public GridLength DepotAssignmentWidth { get { return (hasBoolean) ? new GridLength(50, GridUnitType.Pixel) : GridLength.Auto; } }
System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Windows.GridLength' and 'System.Double'. Consider using Converter property of Binding. BindingExpression:Path=DepotAssignmentWidth; DataItem='RoutesAndDepotsPresenter' (HashCode=39457967); target element is 'GridViewColumn' (HashCode=30364822); target property is 'Width' (type 'Double')  
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='50' BindingExpression:Path=DepotAssignmentWidth; DataItem='RoutesAndDepotsPresenter' (HashCode=39457967); target element is 'GridViewColumn' (HashCode=30364822); target property is 'Width' (type 'Double')

这个,没有给我任何错误,但似乎不起作用。

public double DepotAssignmentWidth { get { return (hasBoolean) ? 50 : GridLength.Auto.Value; } }

实现这一点的正确方法是什么?

试试这个:

public double DepotAssignmentWidth 
{ 
    get 
    { 
        return (hasBoolean) ? 50 : double.NaN; 
    }
}

最新更新