Skip to content

Instantly share code, notes, and snippets.

@papinko
Created September 10, 2024 04:57
Show Gist options
  • Save papinko/5b14a3208458cdcd98db5ace06c8010d to your computer and use it in GitHub Desktop.
Save papinko/5b14a3208458cdcd98db5ace06c8010d to your computer and use it in GitHub Desktop.
Custom DependencyProperty with RaisePropertyChanged-Event
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