如何在WPF中将样式触发器应用于datatemplate

时间:2011-06-07 04:23:13

标签: wpf triggers datatemplate templating

我有以下内容..

<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>

问题是你不能像我想要在DataTemplate中那样应用Style.Trigger。所以我的问题是如何应用创建触发器以使DataTemplate上的属性根据父级更改?

最终解决方案:

我拿了Souvik给我的东西并修好了,因为有一些问题。这是最终结果。

 <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 个答案:

答案 0 :(得分:11)

使用DataTemplate触发器代替Style触发器:

<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>