<Window x:Class="DependencyPropertyDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DrawShape" Height="350" Width="525">
<Grid>
<StackPanel>
<TextBox TextChanged="TextBox_TextChanged" Name="input">0</TextBox>
<Grid>
<Polygon Name="poly" Stroke="Black" Fill="Red"/>
</Grid>
</StackPanel>
</Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DependencyPropertyDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public static readonly DependencyProperty SidesProperty;

public int Sides
{
get { return (int)GetValue(SidesProperty); }
set { SetValue(SidesProperty,value);}
}
static MainWindow()
{
FrameworkPropertyMetadata md = new FrameworkPropertyMetadata();
md.PropertyChangedCallback = OnSidesChanged;
SidesProperty = DependencyProperty.Register("Sides", typeof(int), typeof(MainWindow), md);
}
public MainWindow()
{
InitializeComponent();
}

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
int sideCount;
bool success = int.TryParse(input.Text, out sideCount);
if (success && sideCount > 2)
{
Sides = sideCount;
}
}
static void OnSidesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
MainWindow win = obj as MainWindow;
if (win == null || win.poly == null) return;

const int xCenter = 65;
const int yCenter = 50;
const int radius = 40;
double rds = Math.PI / win.Sides * 2;

win.poly.Points.Clear();
win.poly.Points.Add(new Point(xCenter+radius,yCenter));
for (double i = 1; i <= win.Sides - 1; i++)
{
double x = (Math.Cos(rds * i) * radius) + xCenter;
double y = (Math.Sin(rds * i) * radius) + yCenter;
win.poly.Points.Add(new Point(x, y));
}
}
}
}

DependencyProperty的更多相关文章

  1. WPF中监视DependencyProperty的变化

    WPF中监视DependencyProperty的变化   周银辉 尽管一个类会提供很多事件,但有时候还是显得不够,比如说前两天我就以为WPF的ListBox控件会有ItemsSourceChange ...

  2. [Silverlight]监听指定控件(FrameworkElement)的依赖属性(DependencyProperty)的更改

    前言 转载请注明出处:http://www.cnblogs.com/ainijiutian 最近在silverlight项目使用Telerik的控件,遇到一个问题.就是使用RadBusyIndicat ...

  3. WPF依赖属性DependencyProperty

    写在之前: 依赖属性算是WPF醉醉基础的一个组成了.平时写代码的时候,简单的绑定很轻松,但是遇到复杂的层次比较多的绑定,真的是要命.所以,我觉得深刻认识依赖属性是很有必要的.本篇只是个人学习的记录,学 ...

  4. DependencyProperty深入浅出

    写这篇心得之前,看到博友一句话:需求是推动发展的原动力. 说得好,说的棒,说到了点子上,说到了心里去: 好我们开始 最初的世界是简单的,甚至比单细胞动物还简单: 普通属性定义 public class ...

  5. .NET: WPF DependencyProperty

    DependencyProperty and DependencyObject is the core of WPF data binding. We can use this two class t ...

  6. WPF中的DependencyProperty存储方式详解

    前言 接触WPF有一段时间了,之前虽然也经常使用,但是对于DependencyProperty一直处于一知半解的状态.今天花了整整一下午将这个概念梳理了一下,自觉对这个概念有了较为清晰的认识,之前很多 ...

  7. 未能从文本"Template"创建 "System.Windows.DependencyProperty"

    XXXXProperty = DependencyProperty.Register("XXXX", typeof(double), typeof(MyClass), new Pr ...

  8. WPF 依赖属性源码 洞察微软如何实现DependencyProperty

    依赖属性DependencyProperty是wpf最重要的一个类,理解该类如何实现对学习wpf帮助很大! 终于找到了该类的源码!仔细阅读源码,看看微软如何玩的花招! File: Base\Syste ...

  9. [WPF源码分析]ContentControl依赖项属性的双向绑定,two-way binding view's DependencyProperty and ViewModel's variable

    问题:自定义控件的依赖项属性和VIewModel中的变量不能双向绑定 解决思路:对比.net源码 PresentationFramework  /   System.Windows.Controls ...

随机推荐

  1. ManagerDay-2

    新工作开始了两个星期 基本没有什么产出,主要还是适应新岗位和学东西.作为一个由高级开发转初级PM的人,要学要接触的还有太多. 公司给我安排了一个刚刚起步的项目,可能也是我从业三年接触到的最大的一个项目 ...

  2. JavaScript高级程序设计61.pdf

    JSON对象 早期的JSON解析器就是使用JavaScript的eval()函数,ECMAScript5对解析JSON的行为做出了规定,定义了全局对象JSON. JSON对象有2个方法:stringi ...

  3. MongoDB log4j 日志整合

    在分布式系统当中,这些服务可能分别部署在不同的服务器上,并且有各自的日志输出.为了方便对这些日志进行统一管理和分析.我们可以将日志统一输出到指定的数据库系统中,而再由日志分析系统去管理.而这个储存日志 ...

  4. xml基础学习笔记

    1 XML入门 1.1 引入 HTML: 负责网页的结构 CSS: 负责网页的样式(美观) Javascript: 负责在浏览器端与用户进行交互. 负责静态的网页制作的语言 HTML语言特点: 1)由 ...

  5. [课堂实践与项目]手机QQ客户端--4期(SQLite的加入,注册,找回,登录界面的修改):建立关于QQ注册类,使用SQLite进行存储,

    经过昨天下午和今天上午的不懈努力,终于通过了SQLite的学习. 我们现在这里定义一个有关SQLIte的封装类,便于我在后面的用户注册,用户密码找回,和登录界面的使用 1.首先我们看看我们建立的use ...

  6. C#中npoi操作Excel[版本2.0.1读写2003、2007格式]

    下载npoi2.0.1dll文件:http://download.csdn.net/detail/pukuimin1226/5851747 public static void test1()     ...

  7. Winfrom 如何安全简单的跨线程更新控件

    来源:http://www.cnblogs.com/rainbowzc/archive/2010/09/29/1838788.html 由于多线程可能导致对控件访问的不一致,导致出现问题.C#中默认是 ...

  8. SQL Server与Oracle对比学习:权限管理(二) 一些有趣的比喻

    http://blog.csdn.net/weiwenhp/article/details/8094739 目录(?)[-] SQL Server权限管理 login 与user的区别 角色role ...

  9. [Redux] Colocating Selectors with Reducers

    We will learn how to encapsulate the knowledge about the state shape in the reducer files, so that t ...

  10. Java中NaN和-0.0f的比较问题

    简单的说,比较两个int型或long型的数据没有什么问题,可以用==来判断,但对浮点数(float与double)来说,需要对Float.NaN和0.0这个两个特殊数字作额外的处理.Float.NaN ...