Search code examples
wpfdatagridivalueconverter

Whis is the column not resize?


I have a datagrid and I want to set the width of a column according to some values, so I am trying to use a multibinding in this way:

                        <DataGridTextColumn.Width>
                            <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
                                <Binding Source="{x:Reference ProxyElement}" Path="DataContext.MyProperty" />
                                <Binding Source="0"/>
                            </MultiBinding>
                        </DataGridTextColumn.Width>

The converter is fired, but the width is not changed according to de value it returns.

However, I have a similiar converter to set the visibility and it works as expected:

                        <DataGridTextColumn.Visibility>
                            <MultiBinding Converter="{StaticResource MyConverterVisibilityMultiValueConverter}">
                                <Binding Source="{x:Reference ProxyElement}" Path="DataContext.NombreProveedor" />
                                <Binding Source="0"/>
                            </MultiBinding>
                        </DataGridTextColumn.Visibility>

Why does it work with the visibivilty but not with the width?

Thanks.

EDIT:

I have tried setting the width of the textblock inside the column in this way:

Value converter:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//I have tried both ways, DataGridLength and return 20
    //return new DataGridLength(20, DataGridLengthUnitType.SizeToHeader);
    return 20;
}

Xaml: Option 1, setting the width directly, it works

                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="Width" Value="20"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>

Xaml: option 2: trying with value converter, it doesn't work

                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="Width">
                                <Setter.Value>
                                    <MultiBinding Converter="{StaticResource MyMultiValueConverter}" >
                                        <Binding Source="{x:Reference ProxyElement}" Path="DataContext.Property1" />
                                        <Binding Source="{x:Reference ProxyElement}" Path="DataContext.Property2" />
                                    </MultiBinding>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>

The converter is rised and it returns 20, but the column doesn't take this value.


Solution

  • Ensure that the converter returns a DataGridLength value:

    return new DataGridLength(100, DataGridLengthUnitType.Pixel);