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

WPF(依賴屬性)

編輯:C#入門知識

[html]

<Window x:Class="TestOfFirstDependencyObject.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 > 
        <TextBox x:Name="textBox1" BorderBrush="Black" Margin="5" /> 
        <TextBox x:Name="textBox2" BorderBrush="Black" Margin="5" /> 
        <Button Content="OK" Margin="5" Click="Button_Click" /> 
    </StackPanel> 
</Window> 

<Window x:Class="TestOfFirstDependencyObject.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 >
        <TextBox x:Name="textBox1" BorderBrush="Black" Margin="5" />
        <TextBox x:Name="textBox2" BorderBrush="Black" Margin="5" />
        <Button Content="OK" Margin="5" Click="Button_Click" />
    </StackPanel>
</Window>
[csharp]

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
 
namespace TestOfFirstDependencyObject 

    /// <summary>  
    /// Interaction logic for MainWindow.xaml  
    /// </summary>  
    public partial class MainWindow : Window 
    { 
        private Student stu; 
 
        public MainWindow() 
        { 
            InitializeComponent(); 
 
            stu = new Student(); 
            //Binding binding = new Binding("Text")  
            //                      {  
            //                          Source = textBox1  
            //                      };  
            //BindingOperations.SetBinding(stu, Student.NameProperty, binding);  
 
            stu.SetBinding(Student.NameProperty, new Binding("Text") 
                                                     { 
                                                         Source = textBox1 
                                                     }); 
            textBox2.SetBinding(TextBox.TextProperty, new Binding("Name") 
                                                          { 
                                                              Source = stu 
                                                          }); 
 
        } 
 
        private void Button_Click(object sender, RoutedEventArgs e) 
        { 
            //Student stu = new Student();  
            //stu.SetValue(Student.NameProperty,this.textBox1.Text);  
            //textBox2.Text = stu.GetValue(Student.NameProperty) as string;  
 
            //MessageBox.Show(stu.GetValue(Student.NameProperty).ToString());  
 
            //Student stu = new Student();  
            //stu.Name = textBox1.Text;  
            //this.textBox2.Text = stu.Name;  
        } 
    } 
 
    public class Student : DependencyObject 
    { 
        public static readonly DependencyProperty NameProperty = 
            DependencyProperty.Register("Name", typeof(string), typeof(Student)); 
 
        public string Name 
        { 
            get { return (string)GetValue(NameProperty); } 
            set { SetValue(NameProperty, value); } 
        } 
 
 
 
        public int Age 
        { 
            get { return (int)GetValue(AgeProperty); } 
            set { SetValue(AgeProperty, value); } 
        } 
 
        // Using a DependencyProperty as the backing store for Age.  This enables animation, styling, binding, etc...  
        public static readonly DependencyProperty AgeProperty = 
            DependencyProperty.Register("Age", typeof(int), typeof(Student), new UIPropertyMetadata(0)); 
 
 
 
        public BindingExpressionBase SetBinding(DependencyProperty dp, BindingBase binding) 
        { 
            return BindingOperations.SetBinding(this, dp, binding); 
        } 
 
        public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(int), typeof(Student)); 
 
        public int Id 
        { 
            get { return (int)this.GetValue(IdProperty); } 
            set { this.SetValue(IdProperty, value); } 
        } 
 
 
 
        public string School 
        { 
            get { return (string)GetValue(SchoolProperty); } 
            set { SetValue(SchoolProperty, value); } 
        } 
 
        // Using a DependencyProperty as the backing store for School.  This enables animation, styling, binding, etc...  
        public static readonly DependencyProperty SchoolProperty = 
            DependencyProperty.Register("School", typeof(string), typeof(Student), new UIPropertyMetadata("")); 
 
         
    } 

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace TestOfFirstDependencyObject
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Student stu;

        public MainWindow()
        {
            InitializeComponent();

            stu = new Student();
            //Binding binding = new Binding("Text")
            //                      {
            //                          Source = textBox1
            //                      };
            //BindingOperations.SetBinding(stu, Student.NameProperty, binding);

            stu.SetBinding(Student.NameProperty, new Binding("Text")
                                                     {
                                                         Source = textBox1
                                                     });
            textBox2.SetBinding(TextBox.TextProperty, new Binding("Name")
                                                          {
                                                              Source = stu
                                                          });

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Student stu = new Student();
            //stu.SetValue(Student.NameProperty,this.textBox1.Text);
            //textBox2.Text = stu.GetValue(Student.NameProperty) as string;

            //MessageBox.Show(stu.GetValue(Student.NameProperty).ToString());

            //Student stu = new Student();
            //stu.Name = textBox1.Text;
            //this.textBox2.Text = stu.Name;
        }
    }

    public class Student : DependencyObject
    {
        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(Student));

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

 

        public int Age
        {
            get { return (int)GetValue(AgeProperty); }
            set { SetValue(AgeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Age.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AgeProperty =
            DependencyProperty.Register("Age", typeof(int), typeof(Student), new UIPropertyMetadata(0));

 

        public BindingExpressionBase SetBinding(DependencyProperty dp, BindingBase binding)
        {
            return BindingOperations.SetBinding(this, dp, binding);
        }

        public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(int), typeof(Student));

        public int Id
        {
            get { return (int)this.GetValue(IdProperty); }
            set { this.SetValue(IdProperty, value); }
        }

 

        public string School
        {
            get { return (string)GetValue(SchoolProperty); }
            set { SetValue(SchoolProperty, value); }
        }

        // Using a DependencyProperty as the backing store for School.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SchoolProperty =
            DependencyProperty.Register("School", typeof(string), typeof(Student), new UIPropertyMetadata(""));

       
    }
}


 

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