续这篇测试:https://www.cnblogs.com/huvjie/p/16909290.html
测试页面:
<Window x:Class="MyWPFSimple3.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:MyWPFSimple3"
mc:Ignorable="d"
Title="MainWindow" Height="170" Width="300">
<Grid>
<StackPanel HorizontalAlignment="Center" Margin="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="姓名:" VerticalAlignment="Center"/>
<TextBox BorderBrush="Black" Height="30" Width="150" VerticalContentAlignment="Center"
Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=PersonName}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0 2 0 0">
<TextBlock Text="年龄:" VerticalAlignment="Center"/>
<TextBox BorderBrush="Black" Height="30" Width="150" VerticalContentAlignment="Center"
Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=PersonAge}" />
</StackPanel>
<Button x:Name="btn_ChangeInfo" Click="btn_ChangeInfo_Click" Content="Change Info" Width="186" Margin="0 5 0 0"/>
<Button x:Name="btn_ShowInfo" Click="btn_ShowInfo_Click" Content="Show Info" Width="186" Margin="0 5 0 0"/>
</StackPanel>
</Grid>
</Window>
BehindCode:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 MyWPFSimple3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public string PersonName
{
get
{
return (string)GetValue(PersonNameProperty);
}
set
{
SetValue(PersonNameProperty, value);
}
}
// Using a DependencyProperty as the backing store for PersonName. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PersonNameProperty =
DependencyProperty.Register("PersonName", typeof(string), typeof(MainWindow), new PropertyMetadata("悟空"));
public int PersonAge
{
get
{
return (int)GetValue(PerAgeProperty);
}
set
{
SetValue(PerAgeProperty, value);
}
}
// Using a DependencyProperty as the backing store for PerAge. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PerAgeProperty =
DependencyProperty.Register("PersonAge", typeof(int), typeof(MainWindow), new PropertyMetadata(500));
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void btn_ChangeInfo_Click(object sender, RoutedEventArgs e)
{
PersonName = "玄奘";
PersonAge = 25;
}
private void btn_ShowInfo_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show($"姓名:{PersonName}\r\n年龄:{PersonAge}\r\n");
}
}
}
标签:DependencyProperty,C#,绑定,System,MainWindow,Windows,using,WPF,public
From: https://www.cnblogs.com/huvjie/p/16909417.html