Silverlight:如何在选择时更改 TreeViewItem 文本颜色

发布于 2024-08-11 04:48:05 字数 1142 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上芳菲 2024-08-18 04:48:05

您可以通过使用 Silverlight 的relativesource的自定义实现来完成此操作:

http://www.codeproject.com/Articles/36500/Implementing-RelativeSource-binding-in-Silverlight.aspx

<UserControl.Resources>
    <Converters:BackgroundConverter x:Key="BackgroundConverter"/>
</UserControl.Resources>

    <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"
                                                 FontSize="11"  Text="{Binding Path=Name}" 
                                                 TextWrapping="Wrap" VerticalAlignment="Top">
                                                            <local:BindingHelper.Binding>
                                    <local:BindingProperties TargetProperty="Foreground" SourceProperty="IsSelected"
                                                             Converter="{StaticResource BackgroundConverter}"
                                                             RelativeSourceAncestorType="TreeViewItem"/>
                                </local:BindingHelper.Binding>
                            </TextBlock>

                        </Grid>

                        </control:HierarchicalDataTemplate>
                    </controls:TreeView.ItemTemplate>
                </controls:TreeView>

You can do this by using custom implementation of RelativeSource for Silverlight:

http://www.codeproject.com/Articles/36500/Implementing-RelativeSource-binding-in-Silverlight.aspx

<UserControl.Resources>
    <Converters:BackgroundConverter x:Key="BackgroundConverter"/>
</UserControl.Resources>

    <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"
                                                 FontSize="11"  Text="{Binding Path=Name}" 
                                                 TextWrapping="Wrap" VerticalAlignment="Top">
                                                            <local:BindingHelper.Binding>
                                    <local:BindingProperties TargetProperty="Foreground" SourceProperty="IsSelected"
                                                             Converter="{StaticResource BackgroundConverter}"
                                                             RelativeSourceAncestorType="TreeViewItem"/>
                                </local:BindingHelper.Binding>
                            </TextBlock>

                        </Grid>

                        </control:HierarchicalDataTemplate>
                    </controls:TreeView.ItemTemplate>
                </controls:TreeView>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文