Search code examples
wpftriggersdatatemplatetemplating

How to apply style trigger to datatemplate in WPF


I've got the following..

<ComboBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding ShipperAddresses}" Text="{Binding ShipperAddress}" Margin="85,2,0,2">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBox AcceptsReturn="True" Width="200" Height="100"/>
            <DataTemplate.Resources>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="IsReadOnly" Value="True">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="False"/>
                    </Style.Triggers>
                    </Setter>
                </Style>
            </DataTemplate.Resources>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

The problem is that you can't apply a Style.Trigger like I'm trying to do inside a DataTemplate. So my question is how would you apply create a trigger so that a property on the DataTemplate changes based on the parent?

FINAL SOLUTION:

I took what Souvik gave me and fixed it up since there were a few problems. Here is the end result.

 <ComboBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding ShipperAddresses}" Text="{Binding ShipperAddress}" DisplayMemberPath="Value" Margin="85,2,0,2">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBox AcceptsReturn="True" Width="200" Height="100" Text="{Binding Path=Value}"/>
                    <DataTemplate.Triggers>
                       <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=IsEditable}" Value="False">
                          <Setter Property="IsEnabled" Value="False"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ComboBox.ItemTemplate>
            <ComboBox.Resources>
                <Style TargetType="{x:Type ComboBox}">
                    <Setter Property="IsEditable" Value="True"/>
                    <Style.Triggers>
                        <Trigger Property="IsDropDownOpen" Value="True" >
                            <Setter Property="IsEditable" Value="False"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>

            </ComboBox.Resources>

Solution

  • Have DataTemplate trigger instead of Style trigger:

    <ComboBox Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding ShipperAddresses}" Text="{Binding ShipperAddress}" Margin="85,2,0,2">
        <ComboBox.ItemTemplate> 
            <DataTemplate>
                <TextBox AcceptsReturn="True" Width="200" Height="100"/>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="False">
                        <Setter Property="IsEnabled" Value="False"/>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>