-2

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.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • You must run the debugger and check whether the converter is *always* called when the property MyProperty changes. Then assert the result of the converter each time. The issue must be related to your converter logic or in general to code you have not posted like the converter and the property implementation. So, also ensure MyProperty raises the PropertyChanged event. You should always avoid posting code snippets. We need their context too. – BionicCode Oct 22 '21 at 13:59
  • @Álvaro García: How is your converter implemented? – mm8 Oct 22 '21 at 14:23
  • The value converter is fired. @mm8 for testing, the converter just return for example 10, a very low value to see if it works, but it doesn't. Also I have tried with return DataGridLength(20, pixel) too but it doesn't work too. – Álvaro García Oct 23 '21 at 10:38

1 Answers1

1

Ensure that the converter returns a DataGridLength value:

return new DataGridLength(100, DataGridLengthUnitType.Pixel);
mm8
  • 163,881
  • 10
  • 57
  • 88