This is a migrated thread and some comments may be shown as answers.

Cannot set foreground using item container style selector?

2 Answers 374 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Adnan
Top achievements
Rank 1
Adnan asked on 21 Aug 2016, 09:15 AM

I am trying to set the Foreground color of selected RadTreeViewItem but it cannot be set, though, other elements like Background is working.

<UserControl>
<UserControl.Resources>
<DataTemplate x:Key="AdvancedVariableTemplate">
            <RadioButton Checked="itemCont
<ResourceDictionary>
 
<!-- ItemContainerStyleSelector -->
<Style x:Key="AdvancedVariableItemContainerStyle" TargetType="telerik:RadTreeViewItem" BasedOn="{StaticResource RadTreeViewItemStyle}">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="Foreground" Value="Green"/>
</Style>
</ResourceDictionary>
class AdvVariableContainerStyleSelector : StyleSelector
    {
        private Style darkThemeStyle;
        private Style lightThemeStyle;
 
        public override Style SelectStyle(object item, DependencyObject container)
        {
            Configuration config = UserConfigurations.getConfig();
            if (config.AppSettings.Settings["CurrentTheme"] != null)
            {
                switch (config.AppSettings.Settings["CurrentTheme"].Value.ToString().ToUpper())
                {
                    case "LIGHT":
                        return lightThemeStyle;
                    case "STANDARD":
                        return lightThemeStyle;
                    case "DARK":
                        return darkThemeStyle;
                    default:
                        return null;
                }
            }
            else
                return null;
        }
 
        public Style DarkThemeStyle
        {
            get
            {
                return this.darkThemeStyle;
            }
            set
            {
                this.darkThemeStyle = value;
            }
        }
 
        public Style LightThemeStyle
        {
            get
            {
                return this.lightThemeStyle;
            }
            set
            {
                this.lightThemeStyle = value;
            }
        }
    }
ainerRadio_Checked"
Content="{Binding CustomName}" ToolTip="{Binding TooltipString}"/>
        </DataTemplate>
        <HierarchicalDataTemplate x:Key="AdvancedGroupSeriesRadioTemplate" ItemsSource="{Binding LstAppDataSeries}">
            <TextBlock Text="{Binding CustomName}" FontWeight="Bold" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="AdvancedGroupSideTemplate" ItemsSource="{Binding  LstAppDataSeriesSides}">
            <TextBlock Text="{Binding CustomName}" FontWeight="Bold"/>
        </HierarchicalDataTemplate>
        <VariablesGroup:VariablesHierarchicalTemplate x:Key="AdvancedVariableItemTemplate"
            VariablesGroupsTemplate="{StaticResource  AdvancedGroupSideTemplate}"
            VariableSideTemplate="{StaticResource AdvancedGroupSeriesRadioTemplate}"
            VariableTemplate="{StaticResource AdvancedVariableTemplate}"/>
        <VariablesGroup:AdvVariableContainerStyleSelector x:Key="AdvancedVariableStyleSelector"
            DarkThemeStyle="{StaticResource AdvancedVariableItemContainerStyle}"
            LightThemeStyle="{StaticResource AdvancedVariableItemContainerStyle}"/>
    </UserControl.Resources>
<Grid>
<telerik:RadTreeView x:Name="lstVariablesAdvanced" SelectedItem="{DynamicResource AdvancedVariableItemContainerStyle}" VerticalAlignment="Stretch" Grid.Row="1" Grid.ColumnSpan="2" BorderThickness="0,0,0,0" ScrollViewer.CanContentScroll="False"
IsEditable="True" SelectionMode="Extended" ItemsSource="{Binding LstAppDataSeriesGrouping}" IsDropPreviewLineEnabled="False" IsDragPreviewEnabled="True"
IsDragTooltipEnabled="False" MouseDown="lstVariablesAdvanced_MouseDown"
IsSingleExpandPath="True" IsExpandOnSingleClickEnabled="False"  IsDragDropEnabled="False"
PreviewKeyDown="lstVariables_PreviewKeyDown" IsLineEnabled="True"
ItemTemplateSelector="{StaticResource AdvancedVariableItemTemplate}"
ItemContainerStyleSelector="{StaticResource AdvancedVariableStyleSelector}"             
Margin="10,0,6,0">
</telerik:RadTreeView>
</Grid>
</UserControl>

2 Answers, 1 is accepted

Sort by
0
Adnan
Top achievements
Rank 1
answered on 22 Aug 2016, 01:33 PM

Formatted code.

<UserControl>
<UserControl.Resources>
<DataTemplate x:Key="AdvancedVariableTemplate">
            <RadioButton Checked="itemContainerRadio_Checked" Content="{Binding CustomName}" ToolTip="{Binding TooltipString}"/>
        </DataTemplate>
        <HierarchicalDataTemplate x:Key="AdvancedGroupSeriesRadioTemplate" ItemsSource="{Binding LstAppDataSeries}">
            <TextBlock Text="{Binding CustomName}" FontWeight="Bold" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="AdvancedGroupSideTemplate" ItemsSource="{Binding  LstAppDataSeriesSides}">
            <TextBlock Text="{Binding CustomName}" FontWeight="Bold"/>
        </HierarchicalDataTemplate>
        <VariablesGroup:VariablesHierarchicalTemplate x:Key="AdvancedVariableItemTemplate"
            VariablesGroupsTemplate="{StaticResource  AdvancedGroupSideTemplate}"
            VariableSideTemplate="{StaticResource AdvancedGroupSeriesRadioTemplate}"
            VariableTemplate="{StaticResource AdvancedVariableTemplate}"/>
        <VariablesGroup:AdvVariableContainerStyleSelector x:Key="AdvancedVariableStyleSelector"
            DarkThemeStyle="{StaticResource AdvancedVariableItemContainerStyle}"
            LightThemeStyle="{StaticResource AdvancedVariableItemContainerStyle}"/>
    </UserControl.Resources>
<Grid>
<telerik:RadTreeView x:Name="lstVariablesAdvanced" SelectedItem="{DynamicResource AdvancedVariableItemContainerStyle}" VerticalAlignment="Stretch" Grid.Row="1" Grid.ColumnSpan="2" BorderThickness="0,0,0,0" ScrollViewer.CanContentScroll="False"
IsEditable="True" SelectionMode="Extended" ItemsSource="{Binding LstAppDataSeriesGrouping}" IsDropPreviewLineEnabled="False" IsDragPreviewEnabled="True"
IsDragTooltipEnabled="False" MouseDown="lstVariablesAdvanced_MouseDown"
IsSingleExpandPath="True" IsExpandOnSingleClickEnabled="False"  IsDragDropEnabled="False"
PreviewKeyDown="lstVariables_PreviewKeyDown" IsLineEnabled="True"
ItemTemplateSelector="{StaticResource AdvancedVariableItemTemplate}"
ItemContainerStyleSelector="{StaticResource AdvancedVariableStyleSelector}"            
Margin="10,0,6,0">
</telerik:RadTreeView>
</Grid>
</UserControl>

<ResourceDictionary>
  
<!-- ItemContainerStyleSelector -->
<Style x:Key="AdvancedVariableItemContainerStyle" TargetType="telerik:RadTreeViewItem" BasedOn="{StaticResource RadTreeViewItemStyle}">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="Foreground" Value="Green"/>
</Style>
</ResourceDictionary>

class AdvVariableContainerStyleSelector : StyleSelector
    {
        private Style darkThemeStyle;
        private Style lightThemeStyle;
  
        public override Style SelectStyle(object item, DependencyObject container)
        {
            Configuration config = UserConfigurations.getConfig();
            if (config.AppSettings.Settings["CurrentTheme"] != null)
            {
                switch (config.AppSettings.Settings["CurrentTheme"].Value.ToString().ToUpper())
                {
                    case "LIGHT":
                        return lightThemeStyle;
                    case "STANDARD":
                        return lightThemeStyle;
                    case "DARK":
                        return darkThemeStyle;
                    default:
                        return null;
                }
            }
            else
                return null;
        }
  
        public Style DarkThemeStyle
        {
            get
            {
                return this.darkThemeStyle;
            }
            set
            {
                this.darkThemeStyle = value;
            }
        }
  
        public Style LightThemeStyle
        {
            get
            {
                return this.lightThemeStyle;
            }
            set
            {
                this.lightThemeStyle = value;
            }
        }
    }

0
Dinko | Tech Support Engineer
Telerik team
answered on 25 Aug 2016, 06:18 AM
Hi Adnan,

As it is quite hard to reproduce your case, I could suggest you bind the needed Foregrounds of the TextBlocks and RadioButtons to the Foreground of the RadTreeViewItem or to the Foreground of its ContentPresenter via something like:

Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type telerik:RadTreeViewItem}}, Path=Foreground}"

or

Foreground="{Binding RelativeSource={RelativeSource AncestorType=ContentPresenter}, Path=(TextElement.Foreground)}"

so they can get the value from the Setter in the custom style. Give this approaches a try and let us know if they work for you.

Regards,
Dinko
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
TreeView
Asked by
Adnan
Top achievements
Rank 1
Answers by
Adnan
Top achievements
Rank 1
Dinko | Tech Support Engineer
Telerik team
Share this question
or