2

What I am trying to do is set the number of decimal places for a cell in one column, based on the corresponding row in another column. But I still need the first column to allow sorting after it has been converted.

The first thing I tried was using an IValueConverter. Here is the XAML:

<Window.Resources>
    <src:DataValueConverter x:Key="dataValueConverter" />
</Window.Resources>

<Grid>
    <DataGrid x:Name="dataGrid"  AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Id}"></DataGridTextColumn>
            <DataGridTextColumn Header="Humidity" Binding="{Binding Humidity, Converter={StaticResource dataValueConverter}"></DataGridTextColumn>
            <DataGridTextColumn Header="Version" Binding="{Binding Version}"></DataGridTextColumn>                
        </DataGrid.Columns>
    </DataGrid>  
</Grid>

Here I want the number of decimal places in the humidity value based on the value in version. So I tried passing version to ConverterParameter, and then quickly discovered that it does not accept bindings.

I tried using MultiBinding with MultiValueConverter as suggested Binding ConverterParameter. It did show the different decimal places, however the column could no longer be sorted. Also tried this bindable ConverterParameter http://www.codeproject.com/Articles/456589/Bindable-Converter-Parameter, giving the same results.

So is it possible to fill the ConverterParameter to get the desired results?

<DataGridTextColumn Header="Humidity" Binding="{Binding Humidity, Converter={StaticResource dataValueConverter}, ConverterParameter=???}">

Community
  • 1
  • 1

1 Answers1

1

The solution was to stick with MultiBinding, but also set the SortMemberPath for the humidity column. This tells the column what value to sort by. So the resulting XAML looks like:

 <DataGrid x:Name="dataGrid"  AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Id}"></DataGridTextColumn>
            <DataGridTextColumn Header="Humidity" SortMemberPath="Humidity">
                <DataGridTextColumn.Binding>
                    <MultiBinding Converter="{StaticResource multiDataValueConverter}">
                        <Binding Path="Humidity" />
                        <Binding Path="Version" />
                    </MultiBinding>
                </DataGridTextColumn.Binding>
            </DataGridTextColumn>

Then, the code behind is an IMultiConverter, with something like:

public class MultiDataValueConverter: IMultiValueConverter
    {

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {                    
            int version = (int)(values[1]);

            string format = "0.0000";
            if(version==2)
            {
                format = "0.000000";
            }

            double val = (double)(values[0]);
            return val.ToString(format);            
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }