zoukankan      html  css  js  c++  java
  • 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));
    }
    }
    }
    }

  • 相关阅读:
    bzoj1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
    bzoj1016: [JSOI2008]最小生成树计数
    bzoj1051: [HAOI2006]受欢迎的牛
    bzoj1003: [ZJOI2006]物流运输
    bzoj1079: [SCOI2008]着色方案
    bzoj1179: [Apio2009]Atm
    bzoj1877: [SDOI2009]晨跑
    bzoj1821: [JSOI2010]Group 部落划分 Group
    bzoj1305: [CQOI2009]dance跳舞
    bzoj1858: [Scoi2010]序列操作
  • 原文地址:https://www.cnblogs.com/runningRain/p/5771096.html
Copyright © 2011-2022 走看看