Cannot set foreground using item container style selector?

3 posts, 0 answers
  1. Adnan
    Adnan avatar
    7 posts
    Member since:
    Sep 2013

    Posted 21 Aug Link to this post

    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. Adnan
    Adnan avatar
    7 posts
    Member since:
    Sep 2013

    Posted 22 Aug Link to this post

    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;
                }
            }
        }

  3. UI for WPF is Visual Studio 2017 Ready
  4. Dinko
    Admin
    Dinko avatar
    206 posts

    Posted 25 Aug Link to this post

    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.
Back to Top