SKull
SKull

Reputation: 331

How do I bind a DataGrid Width to a converter?

I want to include the size of the VerticalScrollbar when i define the width of a DataGrid. So far i wrote a Converter:

[ValueConversion(typeof(double), typeof(double))] 
public class VerticalScrollbarConverter : IValueConverter   
{
    #region IValueConverter Member
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {   
        if (value is double)
            return (double)value + SystemParameters.VerticalScrollBarWidth;
        else
            return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();            
    }
    #endregion
}

I included my converter in the xaml namespace with:

xmlns:Core="clr-namespace:Core;assembly=SMS_Core"

And I defined the converter as a (window) resource:

    <Window.Resources>
    <Core:VerticalScrollbarConverter x:Key="VerticalScrollbarConverter"/>
</Window.Resources>

Since all of my DataGrid.Columns have a fixed Value I know the value that i need to pass. How do I tell my GridView Width property in xaml to use the converter?

I know that my question is pretty basic. As you can tell I am very new to WPF.

Thanks in advance for every hint. If you need some more info or context just ask away.

Upvotes: 0

Views: 290

Answers (1)

SKull
SKull

Reputation: 331

The comment from sa_ddam213 didn't exactly solve the problem. But it sure did push me into the right direction. To pass the value automatically I had to do the following: I created a property in the window class called TotalColumnWidth. Returning this:

myDataGrid.Columns.Sum(c => c.ActualWidth); 

The xaml is the following:

Width="{Binding ElementName=_Root, Path=TotalColumnWidth,  Converter={StaticResource ResourceKey=VerticalScrollbarConverter}}"

Using the converter mentioned above.

Upvotes: 1

Related Questions