Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - RelativeSource=/ParentData in Setter or Template?

 
 
Old 01-10-2006   #1 (permalink)
Doug


 
 

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>



My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Output not collected from synthetic setter methods PowerShell
template with Netsh Vista networking & sharing


Vista Forums 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 Ltd

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