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 - Using variable XPaths in Style declarations

 
 
Old 10-25-2007   #1 (permalink)
muchinger


 
 

Using variable XPaths in Style declarations

Hello,

I have this simple piece of XAML (just paste it into XAMLPad and it
will work).


<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

<Canvas.Resources>

<XmlDataProvider x:Key="DataProvider" XPath="Data">
<x:XData>
<Data xmlns="">
<TB Name="TXT_1">
<Background>yellow</Background>
</TB>
<TB Name="TXT_2">
<Background>green</Background>
</TB>
</Data>
</x:XData>
</XmlDataProvider>

</Canvas.Resources>

<Canvas.DataContext>
<Binding Source="{StaticResource DataProvider}" />
</Canvas.DataContext>

<TextBox x:Name="TXT_1" Canvas.Top="0" Height="20" Width="200">
<TextBox.Background>
<Binding XPath="TB[@Name='TXT_1']/Background" />
</TextBox.Background>
</TextBox>

<TextBox x:Name="TXT_2" Canvas.Top="40" Height="20" Width="200">
<TextBox.Background>
<Binding XPath="TB[@Name='TXT_2']/Background" />
</TextBox.Background>
</TextBox>

</Canvas>


Now I looked at how my TextBoxes are declared and I thought to myself:
Well, any TextBox has the same bindings except from the XPath which is
the same apart from the name of the piece of the Xml that I actually
want to be my data source. But on the other hand the value of the
"variable part" in the XPath is always the name of my TextBox.
So I wondered whether I can make all the bindings be part of a style
for TextBoxes. This is what I tried:


<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

<Canvas.Resources>

<XmlDataProvider x:Key="DataProvider" XPath="Data">
<x:XData>
<Data xmlns="">
<TB Name="TXT_1">
<Background>yellow</Background>
</TB>
</Data>
</x:XData>
</XmlDataProvider>

<Style x:Key="txtStyle" TargetType="{x:Type TextBox}">
<Setter Property="TextBox.Background">
<Setter.Value>
<SolidColorBrush Color="{Binding XPath=//TB[@Name\=
\'TXT_1\']/Background}" />
</Setter.Value>
</Setter>
</Style>

</Canvas.Resources>

<Canvas.DataContext>
<Binding Source="{StaticResource DataProvider}" />
</Canvas.DataContext>

<TextBox x:Name="TXT_1" Canvas.Top="0" Height="20" Width="200"
Style="{StaticResource txtStyle}" />

<TextBox x:Name="TXT_2" Canvas.Top="40" Height="20" Width="200"
Style="{StaticResource txtStyle}" />

</Canvas>


Works fine, only that I now have a fixed name in the Binding statement
for the Background property. I do not know how to make this variable.
What I would like to have is something like

<SolidColorBrush Color="{Binding XPath=//TB[@Name\={TemplateBinding
Name}]/Background}" />

Sorry for mixing templates in here, but I thought this would be the
best way to describe what I would like to achieve.

Any suggestions are highly welcome.

Thanks,
Michael


My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
What is the scope of function declarations? PowerShell
Font Size - XP style vs Vista style Vista General
Font Size - XP style vs Vista style Vista General
Font Size - XP style vs Vista style Vista General


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