之前用坐标画多边形的方法,绘制五角星。今天调试时发现当时写的时候有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实现的五角星绘制改进版本,可以绘制半颗五角星,功能更强。 博客文章:http://blog.csdn.net/yysyangyangyangshan/article/details/9313421
WPF-Diagram-Designer:WPF图表设计器源代码
GongSolutions.WPF.DragDrop 一种易于使用的WPF拖放框架。 支持.NET Framework 4.5+和.NET Core 3(3.0和3.1) 产品特点 与MVVM一起使用:拖放的逻辑可以放在ViewModel中。 无需在代码背后放置任何代码,而是将...
对资源:http://download.csdn.net/detail/yysyangyangyangshan/5743911 和...修改一个bug,支持控件的绑定。 文章:http://blog.csdn.net/yysyangyangyangshan/article/details/9378871
gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...
c#-的WPF---MVVM例子,大家共同学习
WPF-Demo:WPF示例源代码
一个简单的WPF程序
vavatech-rittal-wpf-mvvm:Przykładyze szkolenia WPF MVVM
bootstrap-wpf-style-master 样式代码 。。。。。。。。
WPF编程宝典:使用C# 2012和.NET 4.5 第4版 ;高清扫描PDF与源码 打包一起的
WPF-ControlBase OverView 动画封装 属性表单 消息对话 在WPF中应用MVC 其他功能说明 支持
Prism-Samples-Wpf-master06-10的VS2017版本实现,下载手动重新安装一下nuget包即可,方便大家学习
WPF编程宝典:使用C# 2012和.NET 4.5(第4版),完整扫描版. 带书签,经本人整理裁剪适合手机屏,1-3章为清晰版,其它为扫描版
wpf--ListBox分组-排序 wpf--ListBox分组-排序
我最近打算学习WPF ,在寻找MVVM框架的时候发现了PRISM,在此之前还从一些博客上了解了其它的MVVM框架,比如浅谈WPF中的MVVM框架--MVVMFoundation 中提到的MVVMFoundation,再比如 ViewModel从未如此清爽 - 轻量级...
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手
它为 WPF PRISM 应用程序创建一个项目结构,然后继续与 Telerik WPF UI 控件集成,以提供一个停靠和选项卡丰富的用户界面,其中 PRISM 模块被动态注入到选项卡中。 WPF Bootstrap 还为“可换肤”WPF 应用程序提供了...
WPF-:wpf入门系列源码
WPF-Math是一个.NET库,用于WPF框架,使用LaTeX排版样式呈现数学公式。 入门 使用WPF-Math的最简单方法是在XAML文件中呈现静态公式,如下所示。 < Window xss=removed clr-namespace:WpfMath.Controls;assembly=...