views:

98

answers:

3

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={RelativeSource 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.

+1  A: 

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.

bitbonk
Thanks for the reply, could you maybe expand a little on your answer I am fairly new to WPF and a lot of the terms you talk about are new to me.If you could thor in a little mor ecode to extend the example it would be great.Thanks again...
Burt
+1  A: 

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>
esylvestre
Of course, you'll have to ajust the binding on the DataTrigger, but adapted to your solution, it should work.
esylvestre
+1  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>
Burt