Search code examples
wpfwpf-controlswpfdatagriddatatrigger

How to use a data trigger in a text box that uses the selectedItem of a dataGrid?


I want to use a data trigger in a text box to set the isEnabled property according to the value of a property of the selected item in a data grid.

I am trying this:

<TextBox.Style>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <DataTrigger Binding="{Binding MyDataGridName, ElementName=SelectedItem.MyProperty1.MyProperty2}" Value="1">
                <Setter Property="IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBox.Style>

But the textBox is always enabled. I check that really when I select the item, it has property1 and property2 has 1 as value. So I guess that the problem is that I don't set the trigger correctly.


Solution

  • try this code, you need to specify the Datagrid as elementName not the selectedItem:

    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=MyDataGridName, Path=SelectedItem.MyProperty1.MyProperty2}" Value="1">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>