开发者

WPF DataTemplate Trigger set a property in a different DataTemplate

I have 2 DataTemplates (A & B). A contains an Expander and the expander's HeaderTemplate is pointed at another DataTemplate (B).

DataTemplate B is shown below:

    <DataTemplate x:Key="ProjectExpanderHeader">
        <Border CornerRadius="2,2,0,0" 
                Background="{StaticResource ItemGradient}"   
                HorizontalAlignment="{Binding HorizontalAlignment,
                                              RelativeSource={Relat开发者_开发百科iveSource FindAncestor, AncestorType={x:Type ContentPresenter}},
                                              Mode=OneWayToSource}">
            <local:ItemContentsUserControl Height="30"/>
        </Border>
    </DataTemplate>

Is it possible to set the CornerRadius of B's Border when the IsExpanded property of A's Expander is set to true?

Thanks in advance.


You could do this by introducing a new attched property of type CornerRadius (e.g. Helper.CornerRadiusProperty) and attach it to a parent of your ExpanderHeader somewhere in DataTemplate A. You set this property based on IsExpanded using a trigger.

In your DataTemplate B you bind the CornerRadius of your Border to that property using FindAncestor:

<Border CornerRadius="{Binding local:Helper.CornerRadius, 
          RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ContentPresenter}} ...

The above example assumes that you have set the Helper.CornerRadius property on a ContentPresenter in DataTemplate A.


I found my solution. I added the following code to DataTemplateB's Triggers. What it does is look for an ancestor expander control and applies the CornerRadius property to it.

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}},Path=IsExpanded}" Value="false">
                <Setter TargetName="ProjectExpanderHeader" Property="CornerRadius" Value="2,2,2,2"/>
            </DataTrigger>
        </DataTemplate.Triggers>


Why not using the triggers ?

<DataTemplate>
        <Border CornerRadius="2,2,0,0"
                Background="{StaticResource ItemGradient}"
                HorizontalAlignment="{Binding HorizontalAlignment,
                                          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentPresenter}},
                                          Mode=OneWayToSource}">

            <Border.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding DataTemplateA.IsExpanded}"
                                     Value="True">
                            <Setter Property="Border.CornerRadius"
                                    Value="2,2,0,0" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <local:ItemContentsUserControl Height="30" />
        </Border>
    </DataTemplate>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜