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


