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 - Accessing properties of a control in a style declaration

 
 
Old 03-19-2007   #1 (permalink)
muchinger@web.de


 
 

Accessing properties of a control in a style declaration

Data binding gurus out there,

I need your help. I have the following XAML Page:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FormData1;assembly=FormData1"
Title="EntControlTest" Height="176" Width="384">
<Page.Resources>
<local:ValueExtractorConverter x:Key="ValueExtractorConverter"/>
<Style TargetType="{x:Type Label}">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Content">
<Setter.Value>
<!-- this is what i want <Binding
XPath="descendant::Control[@Name='<controlName>']/Value" /> -->
<!-- e. g. <Binding
XPath="descendant::Control[@Name='LBL_2']/Value" /> -->
<!-- but there is not way to inject conditions into an
xpath string (at least not in xaml) -->
<!-- i found a solution where they propose to use a
binding value converter and pass in -->
<!-- the necessary info to the converter via its
parameters -->
<!-- but the question now is: how to i get the name of the
control being "styled" into the parameters? -->
<Binding Converter="{StaticResource
ValueExtractorConverter}" >
<Binding.ConverterParameter>
<local:XmlConverterParameter
XPathTemplate="descendant::Control[@Name='{0}']/
Value"
XPathCondition="LBL_2"
/>
<!-- how can i get e. g. LBL_2 into XPathCondition? --

<!--XPathCondition="{Binding
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type
Label}}}" -->
<!--XPathCondition="{Binding
RelativeSource={RelativeSource Mode=TemplatedParent}}"-->
<!--XPathCondition="LBL_2" -->
</Binding.ConverterParameter>
</Binding>
</Setter.Value>
</Setter>
</Style>

</Page.Resources>

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".3*"/>
<ColumnDefinition Width=".3*"/>
<ColumnDefinition Width=".3*"/>
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<Label Grid.Column="0" x:Name="LBL_1" Content="{Binding
XPath=Control[@Name\=\'LBL_1\']/Value}"/>
<Label Grid.Column="1" x:Name="LBL_2"/>

</Grid>
</Page>

The page is displayed and its DataContext is set to some XML coming
from somewhere.

<Data>
<Control Name="LBL_1">
<Value>This is the 1st Label</Value>
</Control>
<Control Name="LBL_2">
<Value>This is the 2nd Label</Value>
</Control>
</Data>

What I would like to do, is to not set the display text of a label in
the XAML code of the page, but rather let each label in the page data
bind to some XML node.
Now I could write code like
<Label Grid.Column="0" x:Name="LBL_1" Content="{Binding
XPath=Control[@Name\=\'LBL_1\']/Value}"/>
for any of the labels in the page, but I was wondering whether there
is a more elegant solution this and the binding for any label is set
up by a style defined for Label controls. And here is the issue. Since
any of the labels needs to data bind to a different XML node I must
somehow parametrize the XPath in the style so that the XPath
references the correct XML node. Basically the mapping scheme hereby
is: XML node name == Name of the Label control.
Please look at the Page code for things that I already tried to do and
further explanations.

Any suggestions are highly welcome. Thank you very much in advance.

Regards,
Michael


My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
accessing control properties .NET General
Accessing File Properties PowerShell
wmi accessing indexed properties PowerShell


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