之前用坐标画多边形的方法,绘制五角星。今天调试时发现当时写的时候有bug,修改一下。
原文:
http://blog.csdn.net/yysyangyangyangshan/article/details/9313421,当时没测试绑定的问题,一测试发现绑定有问题。原来是多颗五角星控件中,依赖属性的typeof写错了类。

SelectCount和ItemsCount的typeof(FivePointStar)应该为typeof(FivePointStarGroup)才对,顺便将属性的赋值里的代码改为在回调里调用。

修改后如下:

    /// <summary>
/// FivePointStarGroup.xaml 的交互逻辑
/// </summary>
public partial class FivePointStarGroup : UserControl
{
//默认值
private static double radius = 20; private static double itemsCount = 5; private static double selectCount = 5; private static Brush selectBackground = new SolidColorBrush(Colors.YellowGreen); private static Brush unselectBackgroud = new SolidColorBrush(Colors.DarkGray); private static event DependencyPropertyChangedEventHandler PropertyChangedEvent; private static ObservableCollection<FivePointStarModel> data = new ObservableCollection<FivePointStarModel>(); public FivePointStarGroup()
{
InitializeComponent(); this.Loaded += new RoutedEventHandler(FivePointStarGroup_Loaded); PropertyChangedEvent -= new DependencyPropertyChangedEventHandler(FivePointStarGroup_PropertyChangedEvent); PropertyChangedEvent += new DependencyPropertyChangedEventHandler(FivePointStarGroup_PropertyChangedEvent); this.lsbGroups.ItemsSource = data;
} /// <summary>
/// 五角星半径
/// </summary>
public double Radius
{
get
{
object result = GetValue(RadiusProperty); if(result==null)
{
return radius;
} return (double)result;
} set { SetValue(RadiusProperty, value);}
} public static DependencyProperty RadiusProperty =
DependencyProperty.Register("Radius", typeof(double),
typeof(FivePointStarGroup), new UIPropertyMetadata(), new ValidateValueCallback(PaintRadiusElementControls)); public static bool PaintRadiusElementControls(object value)
{
if (PropertyChangedEvent != null)
{
PropertyChangedEvent(1, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));
} return true;
} /// <summary>
/// 五角星个数
/// </summary>
public double ItemsCount
{
get
{
object result = GetValue(ItemsCountProperty); if (result == null || Convert.ToDouble(result )<=0)
{
return itemsCount;
} return (double)result;
} set { SetValue(ItemsCountProperty, value); }
} public static DependencyProperty ItemsCountProperty =
DependencyProperty.Register("ItemsCount", typeof(double),
typeof(FivePointStarGroup), new UIPropertyMetadata(), new ValidateValueCallback(PaintItemCountElementControls)); public static bool PaintItemCountElementControls(object value)
{
if (PropertyChangedEvent != null)
{
PropertyChangedEvent(2, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));
} return true;
} /// <summary>
/// 选中的五角星个数
/// </summary>
public double SelectCount
{
get
{
object result = GetValue(SelectCountProperty); if (result == null || Convert.ToDouble(result) <= 0)
{
return selectCount;
} return (double)result;
} set { SetValue(SelectCountProperty, value);}
} public static DependencyProperty SelectCountProperty =
DependencyProperty.Register("SelectCount", typeof(double),
typeof(FivePointStarGroup), new UIPropertyMetadata(), new ValidateValueCallback(PaintSelectCountElementControls)); public static bool PaintSelectCountElementControls(object value)
{
if (PropertyChangedEvent != null)
{
PropertyChangedEvent(3, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));
} return true;
}
/// <summary>
/// 鼠标点击选中事件
/// </summary>
public event RoutedEventHandler SelectCountChangeEvent
{
add { AddHandler(SelectCountChangePropertyEvent, value); } remove { RemoveHandler(SelectCountChangePropertyEvent, value); }
} public static RoutedEvent SelectCountChangePropertyEvent =
EventManager.RegisterRoutedEvent("SelectCountChangeEvent",
RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Control)); /// <summary>
/// 选中颜色
/// </summary>
public Brush SelectBackground
{
get
{
object result = GetValue(SelectBackgroundProperty); if (result == null)
{
return selectBackground;
} return (Brush)result;
} set { SetValue(SelectBackgroundProperty, value); }
} public static DependencyProperty SelectBackgroundProperty =
DependencyProperty.Register("SelectBackground", typeof(Brush),
typeof(FivePointStarGroup), new UIPropertyMetadata(), new ValidateValueCallback(PaintSelectGroundElementControls)); public static bool PaintSelectGroundElementControls(object value)
{
if (PropertyChangedEvent != null)
{
PropertyChangedEvent(4, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));
} return true;
} /// <summary>
/// 未选中颜色
/// </summary>
public Brush UnSelectBackground
{
get
{
object result = GetValue(UnSelectBackgroundProperty); if (result == null)
{
return unselectBackgroud;
} return (Brush)result;
} set {SetValue(UnSelectBackgroundProperty, value); }
} public static DependencyProperty UnSelectBackgroundProperty =
DependencyProperty.Register("UnSelectBackground", typeof(Brush),
typeof(FivePointStarGroup), new UIPropertyMetadata(), new ValidateValueCallback(PaintUnSelectGroundElementControls)); public static bool PaintUnSelectGroundElementControls(object value)
{
if (PropertyChangedEvent != null)
{
PropertyChangedEvent(5, new DependencyPropertyChangedEventArgs(SelectCountProperty, null, value));
} return true;
} /// <summary>
/// 回调时绘图事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void FivePointStarGroup_PropertyChangedEvent(object sender, DependencyPropertyChangedEventArgs e)
{
if (sender == null || e == null || e.NewValue == null)
{
return;
} int flag = Convert.ToInt32(sender); switch (flag)
{
case 1: InitialData(Convert.ToDouble(e.NewValue), this.ItemsCount, this.SelectCount, this.SelectBackground, this.UnSelectBackground); break; case 2: InitialData(this.Radius, Convert.ToDouble(e.NewValue), this.SelectCount, this.SelectBackground, this.UnSelectBackground); break; case 3: InitialData(this.Radius, this.ItemsCount, Convert.ToDouble(e.NewValue), this.SelectBackground, this.UnSelectBackground); break; case 4: InitialData(this.Radius, this.ItemsCount, this.SelectCount, e.NewValue as Brush, this.UnSelectBackground); break; case 5: InitialData(this.Radius, this.ItemsCount, this.SelectCount, this.SelectBackground, e.NewValue as Brush); break;
} InvalidateVisual();
} void FivePointStarGroup_Loaded(object sender, RoutedEventArgs e)
{
InitialData(this.Radius,this.ItemsCount,this.SelectCount,this.SelectBackground,this.UnSelectBackground);
}
/// <summary>
/// 绘图
/// </summary>
/// <param name="r"></param>
/// <param name="itemcount"></param>
/// <param name="selectcount"></param>
/// <param name="selectbackground"></param>
/// <param name="unselectbackground"></param>
private static void InitialData( double r,double itemcount,double selectcount,Brush selectbackground,Brush unselectbackground)
{
data.Clear(); int count = Convert.ToInt32(itemcount); if (count <= 0)
{
count = Convert.ToInt32(itemsCount);
} for (int i = 0; i < count; i++)
{
FivePointStarModel item = new FivePointStarModel(); item.ID = i + 1; item.Radius = r; item.SelectBackground = selectbackground; item.UnselectBackgroud = unselectbackground; item.Margins = new Thickness(r, 0, r, 0); //在此设置星形显示的颜色
if ((i + 1) > selectcount && ((i + 1 - selectcount) > 0) &&
(i + 1 - selectcount) < 1)
{
item.CurrentValue = 0.5;
}
else if ((i + 1) > selectcount)
{
item.CurrentValue = 0;
}
else
{
item.CurrentValue = 1;
} data.Add(item);
}
} /// <summary>
/// 鼠标选中五角星
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FivePointStar_MouseDown(object sender, MouseButtonEventArgs e)
{
FivePointStar m = sender as FivePointStar; if (m == null)
{
return;
} int index = Convert.ToInt32(m.Tag); this.SelectCount = index; RaiseEvent(new RoutedEventArgs(SelectCountChangePropertyEvent, sender));
}
}

修改后工程下载(包括了绑定测试): http://download.csdn.net/detail/yysyangyangyangshan/5782113

WPF-22:WPF绘制五角星改进版(增加半个五角星的绘制)-修改bug的更多相关文章

  1. 【WPF】WPF截屏

    原文:[WPF]WPF截屏 引言 .NET的截图控件在网上流传得不多啊,难得发现一个精品截图控件( 传送门),但是无奈是winform的.后来又找到一个周银辉做的WPF截图(继续传送门),发现截屏是实 ...

  2. 【WPF】wpf用MultiBinding解决Converter需要动态传参的问题,以Button为例

    原文:[WPF]wpf用MultiBinding解决Converter需要动态传参的问题,以Button为例       用Binding并通过Converter转换的时候,可能偶尔会遇到传参的问题, ...

  3. 基于托管的C++来使用WPF - Using WPF with Managed C++

    基于托管的C++来使用WPF - Using WPF with Managed C++ Posted by Zeeshan Amjad This article was originally publ ...

  4. 双缓冲绘图和窗口控件的绘制——ATL ActiveX 窗口控件生成向导绘制代码OnDraw的一个错误 .

    双缓冲绘图和窗口控件的绘制 ---ATL ActiveX 窗口控件生成向导绘制代码OnDraw的一个错误 cheungmine 我们通常使用ATL COM组件,生成一个带窗口的ActiveX控件,然后 ...

  5. 利用IOS画图功能画出五角星,并且可以调整五角星的填充范围

    我们要花的为一个黄色的五角星并且其中的填充黄色能够任意调整,比如只填满半个五角星,或者只填满一个角等等. 首先要重写DrawRect 方法,然后在这里实现我们的画图代码. - (void)drawRe ...

  6. iOS --SQL的增加、删除、查找、修改

    iOS对于数据库的操作:增加.删除.查找.修改 首先需要创建一个数据库:本程序的数据库是在火狐浏览器里的插件里写的微量型数据库 火狐找查找SQLite Manager的步骤: 第一步:在工具栏找到附加 ...

  7. MySql增加字段、删除字段、修改字段

    MySql增加字段.删除字段.修改字段名称.修改字段类型   1.增加一个字段 alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; / ...

  8. MongoDB增加用户、删除用户、修改用户读写权限及只读权限(注:转载于http://www.2cto.com/database/201203/125025.html)

    MongoDB  增加用户 删除用户  修改用户  读写权限 只读权限,   MongoDB用户权限分配的操作是针对某个库来说的.--这句话很重要.   1. 进入ljc 数据库:       use ...

  9. 22 WPF列表,树,网格

    ListView ListView从ListBox派生,只增加了View属性.如果你没有设置View属性,ListView行为正如ListBox. 从技术上,View属性指向任何ViewBase派生类 ...

随机推荐

  1. 基于Visual C++2013拆解世界五百强面试题--题4-double转换成字符串

    请用C语言实现将double类型数据转换成字符串,再转换成double类型的数据.int类型的数据 想要完成题目中的功能,首先我们的先对系统存储double的格式有所了解. 浮点数编码转换使用的是IE ...

  2. DOC下编译和运行带有包的java类文件

    前言: 带有包名的java类在DOC下编译可以成功,但是运行出错  错误: 找不到或无法加载主类 com.soanl.socket.MyServer D盘temp文件下有个Hello.java文件,包 ...

  3. 一道月薪3W的java面试题 (小明和小强都是张老师的学生,张老师的生日是某月某日,2人都不知道张老师的生日)

    小明和小强都是张老师的学生,张老师的生日是M月N日,2人都知道张老师的生日 是下列10组中的一天,张老师把M值告诉了小明,把N值告诉了小强,张老师问他们知道他的生日是那一天吗? 3月4日 3月5日 3 ...

  4. Spark调研笔记第2篇 - 怎样通过Sparkclient向Spark提交任务

    在上篇笔记的基础上,本文介绍Sparkclient的基本配置及Spark任务提交方式. 1. Sparkclient及基本配置 从Spark官网下载的pre-built包中集成了Sparkclient ...

  5. OS之多线程

    os中引入进程的目的是,为了描述和实现多个程序的并发执行,以改善资源利用率及提高系统的吞吐量. 为什么要引入线程?这是为了减少程序并发执行时系统所付出的额外开销(堆栈切换的开销等),使os具有更好的并 ...

  6. aliyun硬盘挂载

    实在难以忍受公司服务器的网络问题,停用了半年的aliyun服务器今天终于决定启用了. 购买的时候是40G的硬盘空间,首先查了一硬盘情况结果发现有一个分区居然没有挂载.  第一步是创建一个分区 输入命令 ...

  7. 简单的前端js+ajax 购物车框架(入门篇)

    其实,一直想把自己写的一些js给总结下,也许是能力有限不能把它完美结合起来.只能自己默默的看着哪些代码,无能为力. 今天在公司实在没有事做,突然就想到写下商城的购物车的前端框架,当然我这里只有购物车的 ...

  8. HTML之标签

    一.HTML 标签 HTML 标记标签通常被称为 HTML 标签 (HTML tag). •HTML 标签是由尖括号包围的关键词,比如 <html> •HTML 标签通常是成对出现的,比如 ...

  9. Flex中神奇的快速辅助 Ctrl+1

    Adobe Flash Builder 中的快速辅助功能提供基于上下文的辅助,有助于您快速执行任务.通过快速辅助,可以在适用于当前代码段的操作列表中选择一个操作. 要调用快速辅助,请在编辑器的上下文菜 ...

  10. discuz bbs注册,登录流程整理!想打通bbs又不想读一遍代码可以参考一下

    bbs 用户注册流程 第一步: /source/class/class_member.php: on_register注册入口 L602 左右 if(!$activation) {//不为空,说明用户 ...