How to give Style to WPF Toolkit Chart

前端 未结 2 1812
青春惊慌失措
青春惊慌失措 2021-02-06 13:49

\"enter

I am using WPF Toolkit Chart with PieChart in my WPF Application.

I want t

2条回答
  •  天涯浪人
    2021-02-06 14:15

    If you looked at visual tree you find out that you must change Background property of grid and border to change background to transparent (elements highlighted in yellow in the below picture).

    enter image description here

    To do that you can change color in Loaded event. First you must find EdgePanel with name ChartArea and after that you must change color of grid and border. If you want to set also background of Legend to transparent you must find Legend element and set appropriate properties.

                
        
                            
            
                   
    
    

    Code-behind:

    private void mcChart_Loaded(object sender, RoutedEventArgs e)
    {
        EdgePanel ep = VisualHelper.FindChild(sender as Chart, "ChartArea");
        if (ep != null)
        {
            var grid = ep.Children.OfType().FirstOrDefault();
            if (grid != null)
            {
                grid.Background = new SolidColorBrush(Colors.Transparent);
            }
    
            var border = ep.Children.OfType().FirstOrDefault();
            if (border != null)
            {
                border.BorderBrush = new SolidColorBrush(Colors.Transparent);
            }
        }
    
        Legend legend = VisualHelper.FindChild(sender as Chart, "Legend");
        if (legend != null)
        {
            legend.Background = new SolidColorBrush(Colors.Transparent);
            legend.BorderBrush = new SolidColorBrush(Colors.Transparent);               
        }
    }
    

    Helper class to find child element in this case EdgePanel:

    class VisualHelper
    {
        public static T FindChild(DependencyObject parent, string childName) where T : DependencyObject
        {
            if (parent == null) return null;
    
            T foundChild = null;
    
            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                T childType = child as T;
                if (childType == null)
                {
                    foundChild = FindChild(child, childName);
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
                else
                {
                    foundChild = (T)child;
                    break;
                }
            }
            return foundChild;
        }
    }
    

提交回复
热议问题