程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> WPF(附加屬性)

WPF(附加屬性)

編輯:C#入門知識

[html] roperty.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525"> 
    <StackPanel > 
        <Button Content="OK" Click="Button_Click" /> 
    </StackPanel> 
</Window> 

<Window x:Class="TestOfAttachProperty.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel >
        <Button Content="OK" Click="Button_Click" />
    </StackPanel>
</Window>
[csharp] view plaincopyprint?
using System.Windows; 
 
namespace TestOfAttachProperty 

    /// <summary>  
    /// Interaction logic for MainWindow.xaml  
    /// </summary>  
    public partial class MainWindow : Window 
    { 
        public MainWindow() 
        { 
            InitializeComponent(); 
        } 
 
        private void Button_Click(object sender, RoutedEventArgs e) 
        { 
            Human human = new Human(); 
            School.SetGrade(human, 6); 
            int grade = School.GetGrade(human); 
            MessageBox.Show(grade.ToString()); 
        } 
    } 
 
    public class School:DependencyObject 
    { 
 
        public static int GetGrade(DependencyObject obj) 
        { 
            return (int)obj.GetValue(GradeProperty); 
        } 
 
        public static void SetGrade(DependencyObject obj, int value) 
        { 
            obj.SetValue(GradeProperty, value); 
        } 
 
        // Using a DependencyProperty as the backing store for Grade.  This enables animation, styling, binding, etc...  
        public static readonly DependencyProperty GradeProperty = 
            DependencyProperty.RegisterAttached("Grade", typeof(int), typeof(School), new UIPropertyMetadata(0)); 
 
 
    } 
 
    public class Human:DependencyObject 
    { 
         
    } 

using System.Windows;

namespace TestOfAttachProperty
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Human human = new Human();
            School.SetGrade(human, 6);
            int grade = School.GetGrade(human);
            MessageBox.Show(grade.ToString());
        }
    }

    public class School:DependencyObject
    {

        public static int GetGrade(DependencyObject obj)
        {
            return (int)obj.GetValue(GradeProperty);
        }

        public static void SetGrade(DependencyObject obj, int value)
        {
            obj.SetValue(GradeProperty, value);
        }

        // Using a DependencyProperty as the backing store for Grade.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GradeProperty =
            DependencyProperty.RegisterAttached("Grade", typeof(int), typeof(School), new UIPropertyMetadata(0));


    }

    public class Human:DependencyObject
    {
       
    }
}
------------------------------------------------------------------------------------

C#代碼寫布局


[csharp] 
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 TestOfGridCSharp 

    /// <summary>  
    /// Interaction logic for MainWindow.xaml  
    /// </summary>  
    public partial class MainWindow : Window 
    { 
        public MainWindow() 
        { 
            InitializeComponent(); 
 
            InitializeLayout(); 
        } 
 
        private void InitializeLayout() 
        { 
            // 構造Grid  
            Grid grid = new Grid() 
                            { 
                                ShowGridLines = true 
                            }; 
 
            // 定義Grid  
            grid.ColumnDefinitions.Add(new ColumnDefinition()); 
            grid.ColumnDefinitions.Add(new ColumnDefinition()); 
            grid.ColumnDefinitions.Add(new ColumnDefinition()); 
 
            grid.RowDefinitions.Add(new RowDefinition()); 
            grid.RowDefinitions.Add(new RowDefinition()); 
            grid.RowDefinitions.Add(new RowDefinition()); 
 
            // 初始化Button  
            Button button = new Button() 
                                { 
                                    Content = "OK" 
                                }; 
 
            // 設置Button位置  
            Grid.SetColumn(button,1); 
            Grid.SetRow(button,1); 
 
            // 將按鈕放入Grid中  
            grid.Children.Add(button); 
 
            // 將Grid放入窗體中  
            this.Content = grid; 
        }         
    } 

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 TestOfGridCSharp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            InitializeLayout();
        }

        private void InitializeLayout()
        {
            // 構造Grid
            Grid grid = new Grid()
                            {
                                ShowGridLines = true
                            };

            // 定義Grid
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());

            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());

            // 初始化Button
            Button button = new Button()
                                {
                                    Content = "OK"
                                };

            // 設置Button位置
            Grid.SetColumn(button,1);
            Grid.SetRow(button,1);

            // 將按鈕放入Grid中
            grid.Children.Add(button);

            // 將Grid放入窗體中
            this.Content = grid;
        }       
    }
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved