参考文章https://www.cnblogs.com/flypig/articles/2158833.html
这里先介绍格式:
public string MyString
{
get { return (string)GetValue(MyStringProperty); }
set { SetValue(MyStringProperty, value); }
}
public static readonly DependencyProperty MyStringProperty = DependencyProperty.Register("MyString", typeof(string),
typeof(UserControl1), new PropertyMetadata("defaultValue", new PropertyChangedCallback(OnMyStringPropertyChange)));
static void OnMyStringPropertyChange(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
UserControl1 source = (UserControl1)sender;
source.textBox1.Text = args.NewValue.ToString() + args.NewValue.ToString().Length.ToString();
}
这个格式在很多地方都能查到,
其中第一个参数MyString代表的是该属性的名称
中的MyString,能够通过注册找到该字句,但是实际中发现不需要为MyString也能操作成功,测试中将其改为q。在实际的测试中发现
Text="{Binding ElementName=test11, Path=MyString,UpdateSourceTrigger=PropertyChanged}"
Text="{Binding ElementName=test11, Path=q,UpdateSourceTrigger=PropertyChanged}"两者效果一致。
第二个参数typeof(string)代表的是该属性的类型
第三个参数typeof(UserControl1)代表的是该属性所有者的类型
第四个参数 new PropertyMetadata("defaultValue", new PropertyChangedCallback(OnMyStringPropertyChange))代表的是属性元数据注册依赖项属性。
这里有一个小例子方便理解。
用户程序:
<UserControl x:Class="test1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Name="textBox1" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="120" TextChanged="OnTextChanged" />
</Grid>
</UserControl>
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 test1
{
/// <summary>
/// UserControl1.xaml 的交互逻辑
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
//private string _myString;
public string MyString
{
get { return (string)GetValue(MyStringProperty); }
//set { _myString = value; textBox1.Text = _myString; }
set { SetValue(MyStringProperty, value); }
}
public static readonly DependencyProperty MyStringProperty = DependencyProperty.Register("MyString", typeof(string),
typeof(UserControl1), new PropertyMetadata("defaultValue", new PropertyChangedCallback(OnMyStringPropertyChange)));
static void OnMyStringPropertyChange(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
UserControl1 source = (UserControl1)sender;
source.textBox1.Text = args.NewValue.ToString() + args.NewValue.ToString().Length.ToString();
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
//UserControl1.OnMyStringPropertyChange(sender,)
}
}
}
主程序:
<Window x:Class="test1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:test1="clr-namespace:test1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<test1:UserControl1 x:Name="test11" MyString="ggg"></test1:UserControl1>
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,50,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding ElementName=test11, Path=MyString,UpdateSourceTrigger=PropertyChanged}"></TextBox>
</Grid>
</Window>
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 test1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
这里实现的是一个数据绑定,能够通过改变文本框的内容改变两一个文本框的内容,这里可能有人有疑问,直接两个文本框绑定就可以实现。但是这种方式更加灵活,能够自己添加一些自己需要的处理工作。
以上就是全部代码,完整例子我也上传了https://download.youkuaiyun.com/download/yanxiaoyunsana/10974528。
//
这里借个地方记个内容,
Text="{Binding ElementName=test11, Path=MyString,UpdateSourceTrigger=PropertyChanged}"
这里解释一下这个内容,ElementName表示从用户类添加控件名字,Path表示绑定的属性,UpdateSourceTrigger的默认值是Default,其他值有PropertyChanged、LostFocus和Explicit。
Default表示默认的,因为每个空间都有自己特点,所以每个控件的默认是不一样的,例如TextBox的默认是Explicit。
第一个使用的是Explicit,源不会更新除非你手动来操作。正因为这个原因,我在这个TextBox旁边添加了一个按钮,用于手动更新源。在后台代码中,我们看到点击事件处理方法里面只有两行代码,第一行获取目标控件的绑定,第二行调用UpdateSource()方法。
第二个使用的是LostFocus,对于Text绑定来说其实就是一个默认值。也就是说一旦目标控件失去焦点,源就会被更新。
第三个使用的是PropertyChanged,一旦绑定的属性值改变,源会立即更新。本例中文本改变就产生这种效果。