سؤال

لدي textblock داخل الهرمية datatemplate. أحتاج إلى تعيين اللون الأمامي إلى اللون الأحمر عند تحديد 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>
هل كانت مفيدة؟

المحلول

يمكنك القيام بذلك باستخدام التنفيذ المخصص للإقلاعيين من الفضيل:

http://www.codeproject.com/articles/36500/IMPLENTING-RELSORESORCE-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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top