wpf自定义创建图例1(分段式)

2016-11-08 11:54:01来源:CSDN作者:u011097323人点击

第七城市

项目中用到使用wpf创建多个图图例,通过前端添加控件需制作多个,于是自己写了个两个通用的图例,本例为分段式图例,即一个区间赋一个颜色,渐变色图例见wpf自定义创建图例2(渐变式)


wpf窗体设置样式代码:

<Window x:Class="wpfTest.ColorLegend"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="ColorLegend" Height="326" Width="168" Opacity="0.8"        AllowsTransparency="True" Topmost="True" WindowStyle="None" Background="{x:Null}"        AllowDrop="True" MouseLeftButtonDown="DragWindow" Closed="Window_Closed">    <Window.Resources>        <Style x:Key="lab_Style" TargetType="Label">            <Setter Property="Foreground" Value="White"></Setter>            <Setter Property="HorizontalAlignment" Value="Left"></Setter>            <Setter Property="VerticalAlignment" Value="Center"></Setter>            <Setter Property="Height" Value="25"></Setter>            <Setter Property="Width" Value="70"></Setter>        </Style>        <Style x:Key="title_Style" TargetType="Label">            <Setter Property="Foreground" Value="White"></Setter>            <Setter Property="FontWeight" Value="Bold"></Setter>            <Setter Property="FontSize" Value="12"></Setter>            <Setter Property="HorizontalAlignment" Value="Center"></Setter>            <Setter Property="VerticalAlignment" Value="Center"></Setter>            <Setter Property="Height" Value="25"></Setter>            <Setter Property="Width" Value="70"></Setter>        </Style>        <Style x:Key="rec_Style" TargetType="Rectangle">            <Setter Property="Height" Value="15"></Setter>            <Setter Property="Width" Value="40"></Setter>            <Setter Property="VerticalAlignment" Value="Center"></Setter>            <Setter Property="HorizontalAlignment" Value="Right"></Setter>        </Style>    </Window.Resources></Window>


后台代码:

 /// <summary>    /// ColorLegend.xaml 的交互逻辑    /// </summary>    public partial class ColorLegend : Window    {        public ColorLegend(List<ColorValueSKY> colorSet, string title)        {            InitializeComponent();            SolidColorBrush brush = new SolidColorBrush(Colors.Black);            this.Background = brush;            this.Height = colorSet.Count * 22;            this.Width = 120;            this.CreateLegend(colorSet, title);        }        public void CreateLegend(List<ColorValueSKY> colorSet, string title)        {            Grid grid = new Grid();            RowDefinition row0 = new RowDefinition();            row0.Height = new GridLength(22);            grid.RowDefinitions.Add(row0);            for (int i = 0; i < colorSet.Count; i++)            {                RowDefinition row = new RowDefinition();                row.Height = new GridLength(20);// GridLength.Auto;                grid.RowDefinitions.Add(row);                ColumnDefinition column0 = new ColumnDefinition();                column0.Width = new GridLength(50);// GridLength.Auto;                grid.ColumnDefinitions.Add(column0);                ColumnDefinition column1 = new ColumnDefinition();                column1.Width = new GridLength(70);// GridLength.Auto;                grid.ColumnDefinitions.Add(column1);                for (int j = 0; j < 2; j++)                {                    Rectangle rectangle = new Rectangle();                    rectangle.Style = (Style)this.FindResource("rec_Style");                    rectangle.Fill = new SolidColorBrush(Color.FromArgb((byte)colorSet[i].Alpha, (byte)colorSet[i].R, (byte)colorSet[i].G, (byte)colorSet[i].B));                    grid.Children.Add(rectangle);                    Grid.SetRow(rectangle, i + 1);                    Grid.SetColumn(rectangle, 0);                    Label lab = new Label();                    lab.Style = (Style)this.FindResource("lab_Style");                    lab.Content = colorSet[i].Name;                    grid.Children.Add(lab);                    Grid.SetRow(lab, i + 1);                    Grid.SetColumn(lab, 1);                }            }            Label labT = new Label();            labT.Style = (Style)this.FindResource("title_Style");            labT.Content = title;            grid.Children.Add(labT);            Grid.SetRow(labT, 0);            Grid.SetColumn(labT, 0);            Grid.SetColumnSpan(labT, 2);            this.Content = grid;        }        public void DragWindow(object sender, MouseButtonEventArgs args)        {            this.DragMove();        }        private void Window_Closed(object sender, EventArgs e)        {            this.Close();        }            }


colorSetSky类:

 /// <summary>    /// 自定义颜色集    /// </summary>    public class ColorSetSKY    {        private List<ColorValueSKY> m_ColorSkyList=null;        public string Name="";        public List<ColorValueSKY> ColorSkyList        {            get { return this.m_ColorSkyList; }            set { this.m_ColorSkyList = value; }        }        public ColorSetSKY()        {            m_ColorSkyList = new List<ColorValueSKY>();        }        /// <summary>        /// 根据指定数值返回颜色        /// </summary>        /// <param name="currentValue"></param>        /// <returns></returns>        public ColorValueSKY GetPresetColor(float currentValue)        {            ColorValueSKY curColor = null;            foreach (ColorValueSKY colorValue in m_ColorSkyList)            {                if (currentValue <= colorValue.MaxValue && currentValue > colorValue.MinValue)                {                    curColor= colorValue;                }            }            return curColor;        }        ///// <summary>        ///// 返回指定值对应的颜色ARGB值        ///// </summary>        ///// <param name="currentValue"></param>        ///// <returns></returns>        //public int GetPresetColorArgb(float currentValue)        //{        //    foreach (ColorValue colorValue in m_ColorValueSet)        //    {        //        if (colorValue.ValueInScope(currentValue))        //        {        //            return colorValue.CurrentColorValue;        //        }        //    }        //    return Color.Transparent.ToArgb();        //}    }    /// <summary>    /// 颜色图例    /// </summary>    public class ColorValueSKY    {        /// <summary>        /// 红色 0-255        /// </summary>        public int R;        /// <summary>        /// 绿色 0-255        /// </summary>        public int G;        /// <summary>        /// 蓝色 0-255        /// </summary>        public int B;        /// <summary>        /// 透明度 0-255        /// </summary>        public int Alpha;        /// <summary>        /// 最小值        /// </summary>        public double MinValue;        /// <summary>        /// 最大值        /// </summary>        public double MaxValue;        /// <summary>        /// 显示文字        /// </summary>        public string Name;        /// <summary>        /// 颜色图例        /// </summary>        /// <param name="r">红</param>        /// <param name="g">绿</param>        /// <param name="b">蓝</param>        /// <param name="alpha">透明度</param>        /// <param name="min">最小值</param>        /// <param name="max">最大值</param>        /// <param name="name">名称</param>        public ColorValueSKY(int r, int g, int b, int alpha, double min, double max, string name)        {            this.R = r;            this.G = g;            this.B = b;            this.Alpha = alpha;            this.MinValue = min;            this.MaxValue = max;            this.Name = name;        }    }


效果图:






第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台