DependencyProperty
<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的更多相关文章
- WPF中监视DependencyProperty的变化
WPF中监视DependencyProperty的变化 周银辉 尽管一个类会提供很多事件,但有时候还是显得不够,比如说前两天我就以为WPF的ListBox控件会有ItemsSourceChange ...
- [Silverlight]监听指定控件(FrameworkElement)的依赖属性(DependencyProperty)的更改
前言 转载请注明出处:http://www.cnblogs.com/ainijiutian 最近在silverlight项目使用Telerik的控件,遇到一个问题.就是使用RadBusyIndicat ...
- WPF依赖属性DependencyProperty
写在之前: 依赖属性算是WPF醉醉基础的一个组成了.平时写代码的时候,简单的绑定很轻松,但是遇到复杂的层次比较多的绑定,真的是要命.所以,我觉得深刻认识依赖属性是很有必要的.本篇只是个人学习的记录,学 ...
- DependencyProperty深入浅出
写这篇心得之前,看到博友一句话:需求是推动发展的原动力. 说得好,说的棒,说到了点子上,说到了心里去: 好我们开始 最初的世界是简单的,甚至比单细胞动物还简单: 普通属性定义 public class ...
- .NET: WPF DependencyProperty
DependencyProperty and DependencyObject is the core of WPF data binding. We can use this two class t ...
- WPF中的DependencyProperty存储方式详解
前言 接触WPF有一段时间了,之前虽然也经常使用,但是对于DependencyProperty一直处于一知半解的状态.今天花了整整一下午将这个概念梳理了一下,自觉对这个概念有了较为清晰的认识,之前很多 ...
- 未能从文本"Template"创建 "System.Windows.DependencyProperty"
XXXXProperty = DependencyProperty.Register("XXXX", typeof(double), typeof(MyClass), new Pr ...
- WPF 依赖属性源码 洞察微软如何实现DependencyProperty
依赖属性DependencyProperty是wpf最重要的一个类,理解该类如何实现对学习wpf帮助很大! 终于找到了该类的源码!仔细阅读源码,看看微软如何玩的花招! File: Base\Syste ...
- [WPF源码分析]ContentControl依赖项属性的双向绑定,two-way binding view's DependencyProperty and ViewModel's variable
问题:自定义控件的依赖项属性和VIewModel中的变量不能双向绑定 解决思路:对比.net源码 PresentationFramework / System.Windows.Controls ...
随机推荐
- Unity给力插件之ShaderForge(一)
这是一个用来制作shader的插件,也是一个很好的学习shader的工具.这个插件上手很容易,但是要用它来制作理想的Shader,需要下点功夫. 这儿先列举出基础知识,以及我的一些实践.以后我还会继续 ...
- vijosP1159 岳麓山上打水
vijosP1159 岳麓山上打水 链接:https://vijos.org/p/1159 [思路] 迭代加深搜索+完全背包判断. 自己没有思路,看的别人代码. 总体上讲就是不断增大桶的数目并以之为上 ...
- 使用Block来进行页面间的传值
Block语法 定义Block //定义类型 typedef void (^ReceiveMessageBlock)(NSString *); //申明变量 ReceiveMessageBlock t ...
- CC2530芯片介绍
CC2530,体积更小,功能全, C8051内核,使用起来跟平时的单片机没什么两样,就是程序复杂一些,需要一些时日才能看懂,吃透就更长了,而且对于CC2530TI公司提供了一套完整的解决方案,有布线图 ...
- mongoDB启动参数备忘
本文转载 Mongodb启动命令mongod参数说明 mongod的主要参数有: 基本配置 ----------------------------------------------- ...
- PHP开发APP接口(二)
这里将会调用前面博客的数据库连接单例.文件缓存类和开发APP接口(一) <?php // http://app.com/list.php?page-=1&pagesize=12 requ ...
- C++:复制构造函数在什么时候被调用?
这个问题不是疑问了,查了一下国外网站,总结一下.假设Person是一个类,复制构造函数的调用会在以下几种情况下发生: 1.对象在创建时使用其他的对象初始化 Person p(q); //此时复制构造函 ...
- IntelliJ IDEA 配置Jetty
jetty是google app engine 在大量使用的一款服务器软件,不过当然目前还撼动不了tomcat的地位,但是jetty相当轻量级,可以自己灵活定制 资源占用少 ,所以还是有吸引力的,接下 ...
- 【JAVA - SSM】之MyBatis开发DAO
在SSM框架中的DAO层就是MyBatis中的Mapper,Mapper分为两部分:Mapper接口(JAVA文件)和Mapper映射文件(XML文件).DAO开发(Mapper开发)有两种方式:原始 ...
- (转)JS正则表达式元字符
正则 描述 正则 描述 \f 匹配换页符 \t 匹配制表符 \n 匹配换行符 \v 匹配垂直制表符 \r 匹配回车 \s 匹配单个空格,等同于[\f\n\r\t\v]; \S 表示非空格字符 \d 在 ...