How to apply style trigger to datatemplate in WPF

十年热恋 提交于 2019-11-29 01:12:54

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/6260571/how-to-apply-style-trigger-to-datatemplate-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!