René Hankel René Hankel - 4 months ago 26
C# Question

DataGridColumn - How to Bind the Visibility to DataGrid Width

i try to show the user some not so important columns of my datagrid, only when enough space(width of the whole program) available is. so i wanna bind the visibility of the column to the actual with of the datagrid with a converter.
all of my converters, who i wrote works well, BUT in this case happens nothing, means there is no call of the converter. i checked that out by my debug log.

<mui:DataGridTextColumn Header="Kilometer"
Binding="{Binding Path=Kilometer, TargetNullValue='-', FallbackValue='-'}"
Visibility="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, TargetNullValue=Collapsed, FallbackValue=Collapsed, Converter={StaticResource IntAsLimit2VisibilityCollapsed}, ConverterParameter=1300}"

made i something wrong? is it maybe in this combination not possible?


As stated here, DataGrid columns don't lie in Visual tree of DataGrid, and we can't get to outer DataContext using RelativeSource. We can only use x:Reference to an external FrameworkElement.

Also, here is a similar question with a possible solution (you don't need to set a width and will probably avoid a circular reference).