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
-
把int替換成string,改了一處,另外兩處也同時變了(智能)
- 繼續按tab,到MyProperty,改成PersonName
- 繼續,修改ownerclass為MainWindow
-
修改PropertyMetaData(0)為PropertyMetadata(string.Empty)
這是一個DependencyProperty類