シルバー:ツリービューアイテムのテキストの色を変更するにはどのように選択したとき
-
19-09-2019 - |
質問
私はHierarchicalDataTemplate内のTextBlockを持っています。私は、ツリービューアイテムを選択すると赤に前景色を設定する必要があります。
<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>
解決
あなたは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>
所属していません StackOverflow