Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

RelativeSource=/ParentData in Setter or Template?

Closed Thread
 
Thread Tools Display Modes
Old 01-10-2006   #1 (permalink)
Doug
Guest


 

RelativeSource=/ParentData in Setter or Template?

When I access RelativeSource=/ParentData inside style Setters and Templates
I always get null. I can't work out how to rebind the DataContext in these
scenarios.

For example:

<Style x:Key="MyStyle" TargetType="{x:Type Control}">
<Setter Property="DataContext" Value="{Binding RelativeSource=/ParentData}"
/>
</Style>

or:

<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid DataContext="{Binding RelativeSource=/ParentData}" />
</ControlTemplate>
</Setter.Value>
</Setter Property="Template">

How can I reference the current context? I'm trying to do this because I
need to run the current data context through an IValueConverter.

- Doug






A complete example follows, drop the DataContext Setter to see the desired
result.

<?Mapping XmlNamespace="sys" ClrNamespace="System" Assembly="mscorlib"?>
<Window x:Class="EventTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
xmlns:sys="sys"
Title="EventTest"
>

<Window.Resources>
<x:ArrayExtension Type="{x:Type sys:String}" x:Key="Data">
<sys:String>one</sys:String>
<sys:String>two</sys:String>
<sys:String>three</sys:String>
<sys:String>four</sys:String>
</x:ArrayExtension>
<Style x:Key="MasterDetail" TargetType="{x:Type Control}">
<Setter Property="DataContext" Value="{Binding RelativeSource=/PreviousData}"
/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<DockPanel>
<TextBlock TextBlock.FontSize="30" DockPanel.Dock="Right" Text="{Binding
Path=/.}" />
<ListBox ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"
/>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<DockPanel DataContext="{Binding Source={StaticResource Data}}">
<Control Style="{StaticResource MasterDetail}" />
</DockPanel>
</Window>


Old 01-10-2006   #2 (permalink)
Doug
Guest


 

Re: RelativeSource=/ParentData in Setter or Template?

Ok, so it looks like I didn't fully understand what ParentData meant. And
it would seem there is no RelativeSource directly to the current scope? Instead,
the following workaround seems to do what I want.

<ControlTemplate>
<Grid DataContext="{Binding RelativeSource=/TemplatedParent, Path=DataContext}
/>
</ControlTemplate>

- Doug

> When I access RelativeSource=/ParentData inside style Setters and
> Templates I always get null. I can't work out how to rebind the
> DataContext in these scenarios.



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
RelativeSource PreviousData doesn’t work in ListView? Sergey Aldoukhov Avalon 0 06-24-2008 02:05 PM
Template folder cherrybee Live Mail 1 03-16-2008 07:58 PM
Output not collected from synthetic setter methods Catweazle PowerShell 1 12-13-2007 01:54 PM
Why can't I use TemplateBinding in Setter? HolaMan Avalon 2 04-13-2006 01:27 AM
Using a StaticResource in a template Keith Patrick Avalon 3 02-13-2006 09:10 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50