Apply named style to subitems
-
2012年7月25日 上午 09:29
I have a custom TreeView that displays each tree item with a ControlTemplate or whatever. The displayed type is TreeViewExItem. I can define a style locally that is targeted at all TreeViewExItem types. What I'd like to do is define that style, give it a name and then only use it when requested. Like this:
<Style TargetType="{x:Type TreeViewExItem}">
... This is always used
</Style>
<Style TargetType="{x:Type TreeViewExItem}" x:Key="Windows7Theme">
... This is on-demand
</Style>
<TreeViewEx/> This uses the default style
<TreeViewEx ???ItemStyle="Windows7Theme"/> This shall use the additional style as wellSince the tree items are not explicitly present in the XAML but automatically generated from a template at runtime, I cannot specify that style to each item. I can only specify the style at the parent TreeViewEx control. How can I do that to apply the style to its subitems of a certain type?
所有回覆
-
2012年7月26日 上午 04:34版主
Hi LonelyPixel,
What you need is ItemContainerStyle property, you could set this property in your TreeView,
http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemcontainerstyle.aspx
refer to below sample code:
<Grid x:Name="LayoutRoot" Background="White"> <Grid.Resources> <Style x:Key="myTVIStyle" TargetType="TreeViewItem"> <Setter Property="Foreground" Value="Red" /> </Style> </Grid.Resources> <TreeView ItemContainerStyle="{StaticResource myTVIStyle}"> <TreeViewItem Header="Item1" > <TreeViewItem Header="Item1.1" /> </TreeViewItem> <TreeViewItem Header="Item2" /> <TreeViewItem Header="Item3" /> </TreeView> </Grid>this style will apply to first level TreeViewItem, if you want to apply all sub TreeViewItem, you could create two styles, the one is apply to all treeviewitems, and the other is apply to first level.
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已標示為解答 Sheldon _XiaoModerator 2012年8月7日 上午 06:49
-
2012年8月2日 下午 12:30Why does it only apply on the first level of items? How can I tell it to apply on all items in all levels? Do I need to set this property on each item that has subitems? (Remember, all items on all levels are automatically generated through binding.)
-
2012年8月3日 上午 01:54版主
--> How can I tell it to apply on all items in all levels
you have done it, "I can define a style locally that is targeted at all TreeViewExItem types" you said in original post.
<Style TargetType="{x:Type TreeViewExItem}">
... This is always used
< /Style>-->Why does it only apply on the first level of items?
this is "ItemContainerStyle" works way, if you apply "ItemContainerStyle" in datatemplate, this style will only apply to datatemplate's sub level.
You could also try to define your style as a resource in DataTemplate.
Additional, if you want to apply style based on data type, you could use DataTemplateSelector.
http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx
http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector
If above method does not fit for your scenario, I think you have to use a Loaded event,
<Style TargetType="{x:Type TreeViewItem}"> <EventSetter Event="Loaded" Handler="Apply style"/> </Style>then apply specific style when item loaded.
best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
2012年8月3日 上午 09:58
-
2012年8月7日 上午 06:49版主
Hi LonelyPixel,
I am marking your issue as "Answered", if you have new findings about your issue, please let me know.
Best regards,Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

