WPF Datagrid Width Conveter问题

时间:2012-06-06 03:22:50

标签: wpf datagrid width

我想根据列数更改datagrid的宽度。如果我删除/添加任何列,它应该更改datagrid的宽度。

我有控制模板,这里我使用转换器。问题是加载数据网格时只调用一次。我必须使用转换器实现这一点

Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ctrls:RhinoDataGrid}}, Converter={StaticResource DatagridWidthConvtr}}"

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGrid dgObj = (DataGrid)value;
        ScrollViewer scrollViewer = DataGridHelper.FindVisualChild<ScrollViewer>(dgObj);
        Visibility horizontalBarVisibility = scrollViewer.ComputedHorizontalScrollBarVisibility;
        if (horizontalBarVisibility != Visibility.Visible && horizontalBarVisibility != Visibility.Collapsed)
        {
            DataGridLength dgLength = dgObj.Columns[0].Width;
            double dgWidth = (dgObj.Columns.Count * dgLength.Value) + 35;
            return dgWidth;
        }
        else
        {
            return 500;

        }
    }

我尝试在转换器中指定Path =“GridWidth”并获取datagrid的依赖属性,但它给了我一个“无法将System.Double转换为System.Control.Datagrid”的摘要

public static readonly DependencyProperty GridWidthProperty = DependencyProperty.Register(
        "GridWidth",
        typeof(double),
        typeof(RhinoDataGrid));

    public double GridWidth
    {
        get { return (double)this.GetValue(GridWidthProperty); }
        set { this.SetValue(GridWidthProperty, value); }
    }

请帮忙

由于

0 个答案:

没有答案