Access parent ControlTemplate's DataContext from nested ContentControl

Mar 31, 2009 at 9:41 PM
Edited Mar 31, 2009 at 9:44 PM

I'm trying to use a control template for the Calendar control with a IValueConverter to set the color of the text for the CalendarDayButton controls and it actually works. At least for the properties in the template itself. This, for example, works:

[...]
<UserControl
    [DELETIA]

    xmlns:converters="clr-namespace:BusinessEntityLayer.Converters"            
    xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls"            
    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
   
    <UserControl.Resources>
        <converters:DateColorConverter x:Key="DateColorConverter" />

        <!-- CalendarDayButton style template -->
        <Style TargetType="primitives:CalendarDayButton" x:Key="CalendarExDayButtonStyle">
            <!-- Date button properties -->
            <Setter Property="Background" Value="#FFBADDE9"/>
            <Setter Property="MinWidth" Value="5"/>
            <Setter Property="MinHeight" Value="5"/>
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
            
        [DELETIA]
                            <Rectangle x:Name="TodayBackground" RadiusX="1" RadiusY="1" Opacity="0" Fill="#FFAAAAAA"/>
                            <Rectangle x:Name="SelectedBackground" RadiusX="1" RadiusY="1" Opacity="0" Fill="{TemplateBinding Background}"/>
                        <Rectangle x:Name="Background" RadiusX="1" RadiusY="1" Opacity="0" Fill="{Binding Path=Date, Converter={StaticResource DateColorConverter}}"/>
                            <ContentControl
                            x:Name="NormalText"
                            IsTabStop="False"
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            FontSize="{TemplateBinding FontSize}"
                            Margin="5,1,5,1">
                                <ContentControl.Foreground>
                                    <SolidColorBrush x:Name="selectedText" Color="#FFAAFFAA"/>
                                </ContentControl.Foreground>
                            </ContentControl>
                            <Path x:Name="Blackout" Opacity="0" Margin="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" Fill="#FF000000" Stretch="Fill" Data="M8.1772461,11.029181 L10.433105,11.029181 L11.700684,12.801641 L12.973633,11.029181 L15.191895,11.029181 L12.844727,13.999395 L15.21875,17.060919 L12.962891,17.060919 L11.673828,15.256231 L10.352539,17.060919 L8.1396484,17.060919 L10.519043,14.042364 z"/>
                            <Rectangle x:Name="DayButtonFocusVisual" Visibility="Collapsed" IsHitTestVisible="false" RadiusX="1" RadiusY="1" Stroke="#FF45D6FA"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
   
  [DELETIA]
                <local:CalendarEx Name="m_RegistrationsCalendar" CalendarDayButtonStyle="{StaticResource CalendarExDayButtonStyle}"/>
  [DELETIA]
</UserControl>
[...]

The value I want to set however, is the value in the nested ContentControl. The following doesn't work:

[...]
<UserControl

 [DELETIA]

    xmlns:converters="clr-namespace:BusinessEntityLayer.Converters"            
    xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls"            
    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
   
    <UserControl.Resources>
        <converters:DateColorConverter x:Key="DateColorConverter" />

        <!-- CalendarDayButton style template -->
        <Style TargetType="primitives:CalendarDayButton" x:Key="CalendarExDayButtonStyle">
            <!-- Date button properties -->
            <Setter Property="Background" Value="#FFBADDE9"/>
            <Setter Property="MinWidth" Value="5"/>
            <Setter Property="MinHeight" Value="5"/>
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
           
    [DELETIA]
                            <Rectangle x:Name="TodayBackground" RadiusX="1" RadiusY="1" Opacity="0" Fill="#FFAAAAAA"/>
                            <Rectangle x:Name="SelectedBackground" RadiusX="1" RadiusY="1" Opacity="0" Fill="{TemplateBinding Background}"/>
                            <Rectangle x:Name="Background" RadiusX="1" RadiusY="1" Opacity="0" Fill="{TemplateBinding Background}"/>
                            <ContentControl
                            x:Name="NormalText"
                            IsTabStop="False"
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            FontSize="{TemplateBinding FontSize}"
                            Margin="5,1,5,1">
                                <ContentControl.Foreground>
                                    <SolidColorBrush x:Name="selectedText" Color="{Binding Path=Date, Converter={StaticResource DateColorConverter}}"/>
                                </ContentControl.Foreground>
                            </ContentControl>
                            <Path x:Name="Blackout" Opacity="0" Margin="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" Fill="#FF000000" Stretch="Fill" Data="M8.1772461,11.029181 L10.433105,11.029181 L11.700684,12.801641 L12.973633,11.029181 L15.191895,11.029181 L12.844727,13.999395 L15.21875,17.060919 L12.962891,17.060919 L11.673828,15.256231 L10.352539,17.060919 L8.1396484,17.060919 L10.519043,14.042364 z"/>
                            <Rectangle x:Name="DayButtonFocusVisual" Visibility="Collapsed" IsHitTestVisible="false" RadiusX="1" RadiusY="1" Stroke="#FF45D6FA"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
   
  [DELETIA]
                <local:CalendarEx Name="m_RegistrationsCalendar" CalendarDayButtonStyle="{StaticResource CalendarExDayButtonStyle}"/>
 [DELETIA]
</UserControl>
[...]

I suspect this is because the ContentControl doesn't know about the parent template's DataContext I'm trying to access. How do I reference the template's DataContext from there?

Regards,
Søren

 

Mar 31, 2009 at 11:45 PM
Edited Apr 1, 2009 at 3:37 PM
I'm still struggling with this. I think I meay need to use bind using RelativeSource. I've tried combinations of this 

 

 

<SolidColorBrush x:Name="selectedText" Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Date}" />

 

 

 

and this:

<SolidColorBrush x:Name="selectedText" Color="{Binding Path=Date, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ControlTemplate}, Converter={StaticResource DateColorConverter}}"/>

But everything fails with:

XamlParseException: Unknown attribute Color on element SolidColorBrush

Now what is that about? Is this the correct approach?

(FYI: This may have something to do with the Color property not being a DependencyProperty...)

This is a silverlight application. I wasn't aware that that might be an issue but I guess it might. Anyone?

Regards,
Søren

 

 

 

 

Apr 1, 2009 at 11:56 AM
Edited Apr 1, 2009 at 1:24 PM
I can't do relative binding in Silvelight 2?

Well even without relative binding it still doesn't work. Could someone please help me out here? This is driving me nuts!


Alright, I found a hackish solution. It seems the problem is that SolidColorBrush is not part of the visual or logical tree so it doesn't inherit the datacontext and I cannot do relative databinding in Silverlight.


[DELETIA]
 
                            <Rectangle x:Name="TodayBackground" RadiusX="1" RadiusY="1" Opacity="0" Fill="#FFAAAAAA"/>
                            <Rectangle x:Name="SelectedBackground" RadiusX="1" RadiusY="1" Opacity="0" Fill="{TemplateBinding Background}"/>
                            <Rectangle x:Name="Background" RadiusX="1" RadiusY="1" Opacity="0" Fill="{TemplateBinding Background}"/>
                            <ContentControl
                            x:Name="NormalText"
                            IsTabStop="False"
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            FontSize="{TemplateBinding FontSize}"
                            Margin="5,1,5,1" Foreground="{Binding Path=Date, Converter={StaticResource DateColorConverter}}">
                                <ContentControl.BorderBrush>
                                    <SolidColorBrush x:Name="selectedText" Color="#FFAAFFAA"/>
                                </ContentControl.BorderBrush>
                            </ContentControl>
                            <Path x:Name="Blackout" Opacity="0" Margin="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" Fill="#FF000000" Stretch="Fill" Data="M8.1772461,11.029181 L10.433105,11.029181 L11.700684,12.801641 L12.973633,11.029181 L15.191895,11.029181 L12.844727,13.999395 L15.21875,17.060919 L12.962891,17.060919 L11.673828,15.256231 L10.352539,17.060919 L8.1396484,17.060919 L10.519043,14.042364 z"/>
                            <Rectangle x:Name="DayButtonFocusVisual" Visibility="Collapsed" IsHitTestVisible="false" RadiusX="1" RadiusY="1" Stroke="#FF45D6FA"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>



It annoys me I have to do stuff like that though, and it's probably only my lack of understading that forces me into this. If anyone could shed some light on it I'd still appreciate it.

Regards,
Søren