Created
September 10, 2024 04:57
-
-
Save papinko/5b14a3208458cdcd98db5ace06c8010d to your computer and use it in GitHub Desktop.
Custom DependencyProperty with RaisePropertyChanged-Event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using System.Windows; | |
using System.Windows.Controls; | |
public partial class CustomUserControlView : UserControl, INotifyPropertyChanged | |
{ | |
public CustomUserControlView() | |
{ | |
InitializeComponent(); | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
private void RaisePropertyChanged(string propertyName) | |
{ | |
if (this.PropertyChanged != null) | |
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
public static readonly DependencyProperty SetTextProperty = | |
DependencyProperty.Register("SetText", typeof(string), typeof(UserControl1), new | |
PropertyMetadata(default(string), new PropertyChangedCallback(OnSetTextChanged))); | |
public string SetText | |
{ | |
get { return (string)GetValue(SetTextProperty); } | |
set { SetValue(SetTextProperty, value); } | |
} | |
private static void OnSetTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs args) | |
{ | |
if (d is CustomUserControlView c) | |
{ | |
c.RaisePropertyChanged(nameof(c.Text)); | |
c.RaisePropertyChanged(nameof(c.ReadOnly)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment