Font Colour based on Value

5 posts, 0 answers
  1. James
    James avatar
    5 posts
    Member since:
    Jul 2010

    Posted 06 Aug 2010 Link to this post


    I currently have a Loop going through a cube which returns the name (Caption) of its items.
    However as this is an OLAP cube I want to distinguish between user defined and attribute hierarchies. I thought the best way to do this would be to give each one a different color (so give User Defined Hierarchies a blue font and Attribute Hierarchies a red font for example).

    I thought this would be simple but the method I tried below threw alot of errors, while other methods turned all of the items a particular colour rather than just the items I wanted.

    Has anyone got an idea of how I could change an items colour depending on its value??


    Window1.xaml.cs

    foreach (Hierarchy hierarchy in dimension.Hierarchies)
                    {

                        Dimension HierarchyItem = new Dimension();
                        HierarchyItem.Caption = hierarchy.Caption;
                        DimensionItem.SubDimensions.Add(HierarchyItem);
                        HierarchyItem.IsDraggable = true;

                  
    if (HierarchyItem.Caption == "Account Number")
                            HierarchyItem.Caption = Brushes.Red)
                            else if (HierarchyItem.Caption == "Customer Number")
                            HierarchyItem.Caption = Brushes.Blue)

  2. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 11 Aug 2010 Link to this post

    Hello James,

    You can have an observable Brush property on your business class, say

    public Brush TitleBrush { ... }

    Then in the TreeView's ItemTemplate (the HierarchicalDataTemplate) you can bind the Foreground of the TextBlock there to this property:

    Foreground="{Binding TileBrush}"

    Does this work for you?

    All the best,
    Miroslav
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. James
    James avatar
    5 posts
    Member since:
    Jul 2010

    Posted 17 Aug 2010 Link to this post

    Nearly, but I still get an error.....

    HierarchyItem.TitleBrush.Add(TitleBrush)

    I am still trying to get the colour of the text to change depending on its text value (Caption) but it tells me that I cannot convert the observable collection to media brush. 

    Am I just missing something simple?? 

    Regards
    James


    Window1.xaml
    <Window.Resources>
    <HierarchicalDataTemplate x:Key="ColorTemplate" ItemsSource ="{Binding TitleBrush}">
    <TextBlock Foreground="{Binding TitleBrush}"/>
    </HierarchicalDataTemplate>
    </Window.Resources>

    Cube.cs
    public class Dimension
        {
            private string _caption;
            private string _uniqueName;
     
            public Dimension()
            {      
                this.TitleBrush = new ObservableCollection<TitleBrush>();
            }
        
            public ObservableCollection<TitleBrush> TitleBrush
            {
                get;
                set;
            }

    public class TitleBrush
        {
            private object _brush;

            public object Brush
            {
                get { return _brush; }
                set { _brush = value; }
            }
        }


    Window1.xaml.cs


    foreach
    (Hierarchy hierarchy in dimension.Hierarchies)
                   {
                       Dimension HierarchyItem = new Dimension();
                       HierarchyItem.Caption = hierarchy.Caption;
                       HierarchyItem.UniqueName = hierarchy.UniqueName;
                       DimensionItem.SubDimensions.Add(HierarchyItem);
                       HierarchyItem.IsDraggable = true;
                        
                     if (HierarchyItem.Caption == "Account Number")
                     {
            ObservableCollection<Brush> TitleBrush = new ObservableCollection<Brush>();
                    TitleBrush.Add(Brushes.Crimson);
                    HierarchyItem.TitleBrush.Add(TitleBrush);
                       }

                   dimensionRoot.SubDimensions.Add(DimensionItem);
     
               }
     
               treeCube.Items.Add(dimensionRoot);


  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 20 Aug 2010 Link to this post

    Hi James,

    I prepared a sample project implementing Miro's suggestion. Can you take a look at it and let me know if it works for you or if your scenario requires a different approach.

    Regards,
    Tina Stancheva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. James
    James avatar
    5 posts
    Member since:
    Jul 2010

    Posted 23 Aug 2010 Link to this post

    Hi Tina,

    Yes that project can be modified to suit my needs.
    Thanks for the help

    James
Back to Top
UI for WPF is Visual Studio 2017 Ready