WPF数据绑定-依赖属性

MainWindow.xaml

<Window x:Class="DependencyPropertyDemo.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:local="clr-namespace:DependencyPropertyDemo"

mc:Ignorable="d"

x:Name="window"

Title="Dependency Properties Demo" Height="150" Width="300">

<Grid Margin="10">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto"/>

<ColumnDefinition Width="15"/>

<ColumnDefinition Width="*"/>

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

<RowDefinition Height="10"/>

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

 

<!-- Row 0 -->

<TextBlock Text="Your department"

Grid.Row="0" Grid.Column="0"/>

<TextBlock Text=":"

Grid.Row="0" Grid.Column="1"

HorizontalAlignment="Center"/>

<TextBlock Text="{Binding Department, ElementName=window}"

Margin="0 2"

Grid.Row="0" Grid.Column="2"/>

 

<!-- Row 1 -->

<TextBlock Text="Your name"

Grid.Row="1" Grid.Column="0"/>

<TextBlock Text=":"

Grid.Row="1" Grid.Column="1"

HorizontalAlignment="Center"/>

<TextBox Text="{Binding PersonName, ElementName=window, Mode=TwoWay}"

Margin="0 2"

Grid.Row="1" Grid.Column="2"/>

 

<!-- Row 3 -->

<StackPanel Orientation="Horizontal"

HorizontalAlignment="Center"

Grid.Row="3" Grid.Column="0"

Grid.ColumnSpan="3">

<Button Content="Submit"

Margin="4" Width="80"

Click="OnSubmit"/>

<Button Content="Reset"

Margin="4" Width="80"

Click="OnReset"/>

</StackPanel>

</Grid>

</Window>

 

属性Department

public string Department

{

get { return "Software Engineering"; }

}

 

 

自动生成依赖属性的方法

 

步骤:

在代码编辑器里面打入 propdp,顾名思义,prop=property,dp=dependency property

两次tab键盘,此时选中的是int

  1. 把int替换成string,改了一处,另外两处也同时变了(智能)

  2. 继续按tab,到MyProperty,改成PersonName

  1. 继续,修改ownerclass为MainWindow

  1. 修改PropertyMetaData(0)为PropertyMetadata(string.Empty)

    这是一个DependencyProperty类

posted @ 2020-05-27 22:01  司徒无名  阅读(966)  评论(0编辑  收藏  举报