Hello,
Maybe this is more of a "WPF in general " question but since I need it for my XamDataTree, it may also be related to this.
Basically, I need to apply a particular style to my nodes, by code.
Here is my tree in xaml :
<UserControl.Resources>
<Style x:Key="arretGeocode" TargetType="TextBlock">
<Setter Property="Foreground" Value="Black"></Setter>
</Style>
<Style x:Key="arretNonGeocode" TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"></Setter>
</UserControl.Resources>
<ig:XamDataTree Name="treeViewPoints" VerticalAlignment="Top" NodeLineVisibility="Visible" >
<ig:XamDataTree.GlobalNodeLayouts>
<ig:NodeLayout Key="MyLayout" TargetTypeName="GroupePointNavigateur">
<ig:NodeLayout.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Data.Groupe.Code}" Style="{StaticResource arretGeocode}"/>
<TextBlock Text=" - " Style="{StaticResource arretGeocode}"/>
<TextBlock Text="{Binding Data.Groupe.Nom}" Style="{StaticResource arretGeocode}"/>
</StackPanel>
</DataTemplate>
</ig:NodeLayout.ItemTemplate>
</ig:NodeLayout>
<ig:NodeLayout Key="MyChildLayout" TargetTypeName="VersionPoint">
<TextBlock Text="{Binding Data.Point.Code}" Style="{StaticResource arretGeocode}"/>
<TextBlock Text="{Binding Data.Nom}" Style="{StaticResource arretGeocode}"/>
</ig:XamDataTree.GlobalNodeLayouts>
</ig:XamDataTree>
(Sorry for the style, I still dont understand how to put code samples in your forums...)
So, what I need to do, is to apply the "arretNonGeocode" style to the textblocks of a node, in the C# code behind.
I dont have any problem to get the XamDataTreeNodes, it's just that I can't figure out how to change their itemtemplate when I'm in the code behind.
Thank you in advance.
I am just checking if you got this worked out or you still require any assistance or clarification on the matter.
You can change the Binding of the TextBox’ Foreground to this one :
Foreground="{Binding Data,Converter={StaticResource con}}"
And this way in the Converter you will have the reference to the underlying object. Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
Thank you for your response.
Your solution works fine for me ! The only problem is that my criteria for changing the Foreground depends on the "code" value of my object contained in the node and on a list which is in the treeView class.
So when I'm in the Convert(...) method, I cant access this list.
But I think I'll find a work around.
Thank you again.
I have been looking into your requirement and I modified the sample I sent you before, so now it has the functionality you need. If I understand you correct, you want to change the Foreground of a particular node if the data meets some criteria. To achieve that I used Converter in the Binding of the Foreground of the TextBlock and checked the Text of the TextBlock. Please let me know if this helps you or you need further assistance on this matter.
Hello Stefan,
I tried your solution and it works fine.
But I think I was not clear enough in my post.
In fact, I want to be able to change the color of a particular node in the tree.
Not changing all the NodeLayout template.
I tried several things, changing your code to make it work but since I'm kind of a newbie in WPF, I didn't succeed.
Maybe I need to put a Control on the node and change the ControlTemplate of this control ?