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));
}
}
}
}

 

转载于:https://www.cnblogs.com/runningRain/p/5771096.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值