WPF (additional attributes)

Source: Internet
Author: User

[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
{

}
}
Bytes ------------------------------------------------------------------------------------

C # code writing Layout


[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 ()
{
// Construct a Grid
Grid grid = new Grid ()
{
ShowGridLines = true
};
 
// Define the 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 ());
 
// Initialize the Button
Button button = new Button ()
{
Content = "OK"
};
 
// Set the Button position
Grid. SetColumn (button, 1 );
Grid. SetRow (button, 1 );
 
// Put the button in the Grid
Grid. Children. Add (button );
 
// Put the Grid in the form
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 ()
{
// Construct a Grid
Grid grid = new Grid ()
{
ShowGridLines = true
};

// Define the 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 ());

// Initialize the Button
Button button = new Button ()
{
Content = "OK"
};

// Set the Button position
Grid. SetColumn (button, 1 );
Grid. SetRow (button, 1 );

// Put the button in the Grid
Grid. Children. Add (button );

// Put the Grid in the form
This. Content = grid;
}
}
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.