wpfbindingdatatriggerwpf-grid

How to get a Grid.Column value in a binding expression


I need set background property by Grid.Column value. My idea was simple DataTrigger like this

<DataTrigger Binding="{Binding Grid.Column, ElementName=MyElement}" Value="2">
        <Setter Property="Background" Value="#F2F2F2"/>
</DataTrigger>

MyElement is a generice wpf control for example this:

<Border x:Name="MyElement" Grid.Column="2">
      Content
</Border>

But this is not working and Visual Studio says: Cann't resolve property Grid

I can use C# Grid.GetColumn(MyElement) as workaround. But I'm wondering, is there any xaml only solution here?


Solution

  • To bind to an attached property, place parentheses around the attached property.

    <DataTrigger Binding="{Binding Path=(Grid.Column), ElementName=MyElement}" Value="2">
            <Setter Property="Background" Value="#F2F2F2"/>
    </DataTrigger>