Silverlight:如何在选择时更改 TreeViewItem 文本颜色
我在 HierarchicalDataTemplate 中有 TextBlock。当选择 TreeViewItem 时,我需要将前景色设置为红色。
<controls:TreeView Background="#FF939597"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" x:Name="CommentTreeView" Margin="0,0,0,118"
ItemContainerStyle="{StaticResource SectionsTreeViewItemStyle}">
<controls:TreeView.ItemTemplate>
<control:HierarchicalDataTemplate ItemsSource="{Binding SubSections}" ItemContainerStyle="{StaticResource SectionsTreeViewItemStyle}">
<Grid>
<TextBlock x:Name="ItemTextBlock" Margin="0,6,48,0"
<!-- ??? Foreground="Red" ??? if item selected ??? -->
FontSize="11" Text="{Binding Path=Name}"
TextWrapping="Wrap" VerticalAlignment="Top">
</TextBlock>
</Grid>
</control:HierarchicalDataTemplate>
</controls:TreeView.ItemTemplate>
</controls:TreeView>
I have TextBlock inside HierarchicalDataTemplate. I need to set foreground color to Red when TreeViewItem selected.
<controls:TreeView Background="#FF939597"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" x:Name="CommentTreeView" Margin="0,0,0,118"
ItemContainerStyle="{StaticResource SectionsTreeViewItemStyle}">
<controls:TreeView.ItemTemplate>
<control:HierarchicalDataTemplate ItemsSource="{Binding SubSections}" ItemContainerStyle="{StaticResource SectionsTreeViewItemStyle}">
<Grid>
<TextBlock x:Name="ItemTextBlock" Margin="0,6,48,0"
<!-- ??? Foreground="Red" ??? if item selected ??? -->
FontSize="11" Text="{Binding Path=Name}"
TextWrapping="Wrap" VerticalAlignment="Top">
</TextBlock>
</Grid>
</control:HierarchicalDataTemplate>
</controls:TreeView.ItemTemplate>
</controls:TreeView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过使用 Silverlight 的relativesource的自定义实现来完成此操作:
http://www.codeproject.com/Articles/36500/Implementing-RelativeSource-binding-in-Silverlight.aspx
You can do this by using custom implementation of RelativeSource for Silverlight:
http://www.codeproject.com/Articles/36500/Implementing-RelativeSource-binding-in-Silverlight.aspx