0

I am trying to attach a datatrigger on one of my combobox element. The trigger should read the attached property of the DataGridTextColumn (combobox's ancestor) and take decision based on that. Now the problem is that the DataGridTextColumn isn't part of Visual Tree so I cannot get it by RelativeSource Ancestor. Here is sample code.

<ComboBox Name="cmbFilter" DisplayMemberPath="CategoryName">
    <ComboBox.Style>
        <Style>
            <Style.Triggers>
               <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridTextColumn}}, 
                             Path=Header}"
                             Value="Id">
                    <Setter Property="Control.Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
</ComboBox>

Any one maybe suggest some alternative

Edit:

@denis I don't really understand your solution, so let me explain in detail. I am developing a general purpose filter that would apply to all DataGrid's that need the filtering functionality. The DataGrid definition will specify whether it wants filtering or not by specifying custom attached property "IsFilterable" on DataGrid. Individual DataGridColum will specify what kind of filter they want (combobox or textbox) by specifying "FilterDisplayType" on DataGridColumn. The DataGrid will know nothing other than above. All the functionality will then be handled by the Filter based on the above attached properties on DataGrid and DataGridColumn (all types of columns).

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jatin
  • 4,023
  • 10
  • 60
  • 107
  • How do I find the enclosing DataGridTextColumn. Trigger doesn't even support binding and if I try to use it on Property attribute of Trigger, it throws XmlParseException saying that binding cannot be set on "Property" attribute of Trigger. – Jatin Apr 10 '12 at 12:50
  • How are you changing the Header, obviously you do, otherwise you would have to respond to it with a trigger? What I am hinting/leading to is that you probably can Bind/Trigger on the same property with the ComboBox as on the Header – denis morozov Apr 10 '12 at 16:25
  • I am sorry for having mislead you. The sample code was just for example. Actually, there is a custom attached property on DataGridTextColumn against which I have to write a trigger. Since the trigger is going to make ComboBox invisible, it seems logical to define the trigger in Combobox's style. – Jatin Apr 10 '12 at 16:30

1 Answers1

0

My point in my comment was that, you can trigger on a property that you bind to, not on the header.. Because if you respond to a header change, than you have to trigger the header, which is fine, but that can be the exact same Property on your model that you respond to, only in a diff place.

Also you can't put a combo box in DataGridTextColumn, So you'd have to either:

<DataGridComboBoxColumn ItemsSource="{Binding CategoryNameItems}" 
                        DisplayMemberPath="{Binding CategoryName}" 
                        Visibility="{Binding MyVisibilityProperty, Converter={StaticResource BoolToVisibility}}" />

Which will hide the whole column or to hide only the combobox put it in the CellTemplate:

<DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding CategoryNameItems}" DisplayMemberPath="{Binding CategoryName}" 
                                Visibility="{Binding MyVisibilityProperty, Converter={StaticResource BoolToVisibility}}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
denis morozov
  • 6,236
  • 3
  • 30
  • 45