using System; using System.Windows; namespace Test { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow () { InitializeComponent(); // myControl 是 MainWindow.xaml 中定义的控件实例 // 对应创建的自定义控件 MyControl myControl.MyTitle = "我是窗口" ; } private void Button_Click (object sender, RoutedEventArgs e) { // 点击按钮时更新依赖属性 myControl.MyTitle = textBox.Text; }
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:Test" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <StackPanel Margin="20"> <!-- 利用依赖属性生成一个自定义控件myControl --> <!-- local:MyControl x:Name="myControl" => 在Test命名空间中找一个叫MyControl的类 --> <!-- 这里的依赖属性是Title --> <local:MyControl x:Name="myControl" MyTitle="初始标题" Height="50"/> <!-- 一个文本框 --> <TextBox x:Name="textBox" Margin="0,10,0,0" Height="30"/> <!-- 一个按钮 -->
// 输入propdp,然后连续按两下Tab键,VS会帮我们自动生成下面的这段代码 public int MyProperty { get { return ( int )GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. // This enables animation, styling, binding, etc... public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register( "MyProperty" ,
public static int GetMyProperty (DependencyObject obj) { return ( int )obj.GetValue(MyPropertyProperty); } public static void SetMyProperty (DependencyObject obj, int value) { obj.SetValue(MyPropertyProperty, value); } // Using a DependencyProperty as the backing store for MyProperty. //This enables animation, styling, binding, etc... public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached( "MyProperty" ,
/* MainWindow.xaml.cs */ using System.Windows; namespace TT { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow () { InitializeComponent(); } private void Button_Click (object sender, RoutedEventArgs e) { // 获取附加属性值 string password = PasswordHelper.GetPassword(pwdBox); // 消息弹窗打印 MessageBox.Show($ "当前密码:{password}" );
using System; using System.Windows; using System.Windows.Controls; namespace TT { public static class PasswordHelper { public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached( "Password" , typeof( string ), typeof(PasswordHelper), // string.Empty相当于默认值 = "" // C/C++程序员请注意:C#中 "" ≠ null // new PropertyMetadata(string.Empty, OnPasswordChanged)); new PropertyMetadata(null, OnPasswordChanged)); public static string GetPassword (DependencyObject obj) { return ( string )obj.GetValue(PasswordProperty); } public static void SetPassword (DependencyObject obj, string value) { obj.SetValue(PasswordProperty, value); } // 改变回调 private static void OnPasswordChanged (DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is PasswordBox passwordBox) { // 同步到真实 Password if (passwordBox.Password != ( string )e.NewValue) { passwordBox.Password = ( string )e.NewValue; } // 注册事件(先移除,避免重复) passwordBox.PasswordChanged -= PasswordBox_PasswordChanged; passwordBox.PasswordChanged += PasswordBox_PasswordChanged; } }gimqsb.txzf-pay.com private static void PasswordBox_PasswordChanged (object sender, RoutedEventArgs e) {
PasswordBox.Password ←→ 附加属性 Password ←→ ViewModel.VMod.Password MVVM ├─ App.xaml # 应用入口:声明应用级资源并指定启动窗口 │ ├─ Views │ ├─ MainWindow.xaml # 界面声明:定义 PasswordBox,并通过附加属性绑定 ViewModel 的 Password │ └─ MainWindow.xaml.cs # 界面逻辑:初始化窗口并设置 DataContext │ ├─ ViewModel │ └─ VMod.cs # 视图模型:定义 Password 状态并实现属性变更通知 │
<Window x:Class="MVVM.Views.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:MVVM.Helpers" mc:Ignorable="d" rsvcao.txzf-pay.com Title="MainWindow" Height="450" Width="800"> <StackPanel Margin="20"> <PasswordBox local:PasswordHelper.Password="{Binding Password, Mode=TwoWay}" /> <Button Content="读取密码" Margin="0,10,0,0" Click="Button_Click"/>
using MVVM.ViewModel; using MVVM.Helpers; namespace MVVM.Views { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow () { InitializeComponent(); DataContext = new VMod(); } private void Button_Click (object sender, RoutedEventArgs e) { var vm = (VMod)DataContext; MessageBox.Show(vm.Password);
using System.Windows;tk.txzf-pay.com using System.Windows.Controls; namespace MVVM.Helpers { public static class PasswordHelper { public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached( "Password" , typeof( string ),wap.txzf-pay.com typeof(PasswordHelper),m.txzf-pay.com new FrameworkPropertyMetadata( string .Empty, //null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPasswordChanged));txzf-pay.com public static void SetPassword (DependencyObject obj, string value) { obj.SetValue(PasswordProperty, value);stuczd.txzf-pay.com } public static string GetPassword (DependencyObject obj) { return ( string )obj.GetValue(PasswordProperty); }wap.kruez.com private static void OnPasswordChanged (DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not PasswordBox box) return ; box.PasswordChanged -= PasswordBox_PasswordChanged; if (box.Password != ( string )e.NewValue) { obq.txzf-pay.com box.Password = ( string )e.NewValue;rfux.txzf-pay.com } lnkwbi.txzf-pay.com box.PasswordChanged += PasswordBox_PasswordChanged; }m.kruez.com private static void PasswordBox_PasswordChanged (object sender, RoutedEventArgs e) { var box = (PasswordBox)sender; SetPassword(box, box.Password);
sing System.ComponentModel; namespace MVVM.ViewModel { public class VMod: INotifyPropertyChanged { private string ? _password; public string ? Password { get => _password; set { if (_password == value) return ; _password = value;ldozxw.txzf-pay.com OnPropertyChanged(nameof(Password)); } }kruez.com public event PropertyChangedEventHandler? PropertyChanged; rrs.txzf-pay.com protected void OnPropertyChanged ( string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048957/viewspace-3109431/ ,如需转载,请注明出处,否则将追究法律责任。