I need to raise an event when the value of the property is changed. In my case this is when webView.Source is changed. I can't make a derived class because the class is marked as sealed. Is there any way to raise an event ?
Thank you.
Raise event when property is changed
For this scenario, you could create a DependencyPropertyWatcher to detect DependencyProperty changed event. The follow is tool class that you could use directly.
public class DependencyPropertyWatcher<T> : DependencyObject, IDisposable
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value",
typeof(object),
typeof(DependencyPropertyWatcher<T>),
new PropertyMetadata(null, OnPropertyChanged));
public event DependencyPropertyChangedEventHandler PropertyChanged;
public DependencyPropertyWatcher(DependencyObject target, string propertyPath)
{
this.Target = target;
BindingOperations.SetBinding(
this,
ValueProperty,
new Binding() { Source = target, Path = new PropertyPath(propertyPath), Mode = BindingMode.OneWay });
}
public DependencyObject Target { get; private set; }
public T Value
{
get { return (T)this.GetValue(ValueProperty); }
}
public static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
DependencyPropertyWatcher<T> source = (DependencyPropertyWatcher<T>)sender;
if (source.PropertyChanged != null)
{
source.PropertyChanged(source.Target, args);
}
}
public void Dispose()
{
this.ClearValue(ValueProperty);
}
}
Usage
var watcher = new DependencyPropertyWatcher<string>(this.MyWebView, "Source");
watcher.PropertyChanged += Watcher_PropertyChanged;
private void Watcher_PropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}
You can use a decorator to wrap the original class and raise an event for decorated properties.
Related
I created a DependencyObject with properties in it, I also have it inherit INotifyPropertyChanged.
But when it is implemented as a DependencyProperty, it does not trigger PropertyChangedCallback when I change a single property from the DependencyObject both in Design and Code.
This is the Dependency Object that I will use for my CustomControl.
public class Basemap : DependencyObject, INotifyPropertyChanged
{
private string identifier;
private string name;
private string alias;
private string url;
private Map.DetailRange detailrange;
private Map.Attribution attribution;
public string Identifier
{
get => identifier;
set
{
identifier = value;
OnPropertyChanged("Identifier");
}
}
public string Name
{
get => name;
set
{
name = value;
OnPropertyChanged("Name");
}
}
public string Alias
{
get => alias;
set
{
alias = value;
OnPropertyChanged("Alias");
}
}
public string URL
{
get => url;
set
{
url = value;
OnPropertyChanged("URL");
}
}
public Map.DetailRange DetailRange
{
get => detailrange;
set
{
detailrange = value;
OnPropertyChanged("DetailRange");
}
}
public Map.Attribution Attribution
{
get => attribution;
set
{
attribution = value;
OnPropertyChanged("Attribution");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
It has a PropertyChangedEventHandler and Invoking it whenever the OnPropertyChanged is called, just like in this GUIDE
This it the DependecyProperty I implimented into the CustomControl that has a PropertyChangedCallback.
public static DependencyProperty BasemapProperty = DependencyProperty.Register("Basemap", typeof(Basemap), typeof(Browser), new PropertyMetadata(null, BasemapChanged));
private static void BasemapChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("Basemap Changed");
}
But the PropertyChangedCallback is not triggered when an individual property's value is changed, but it is triggered only when the whole DependecyObject is updated or inserted. Both in Design and Code
<Window.Resources>
<mMAP:Basemap x:Key="OpenStreetMap"
Identifier="OpenStreetMap.Standard"
Name="OpenStreetMap Standard"
Alias="OpenStreetMap"
URL="https://tile.openstreetmap.org/{z}/{x}/{y}.png"
DetailRange="0,0,18"
Attribution="© OpenStreetMap contributors, CC-BY-SA" />
</Window.Resources>
Can anyone suggest any fixes to this?
Thank you.
Edit:
I assignend the PropertyChanged event from the DependecyObject to the control. Basemap.PropertyChanged += Basemap_PropertyChanged; so it can be triggered every time any member of the DependecyObject class. It may be sacrilegious to do it to WPF, but works for me.
PS:
I needed it to be grouped, for it is a configuration for the CustomControl.
You are not binding to the Dependency Property you defined in the control, that is why the PropertyChangedCallback BasemapChanged is not triggered.
While it works on some cases, I try to avoid implementing the INotifyPropertyChanged Interface on Controls and use only Dependency Properties instead when binding is required.
public static DependencyProperty AliasProperty = DependencyProperty.Register(
nameof(Alias),
typeof(string),
typeof(Basemap),
new PropertyMetadata(null, AliasPropertyChangedCallback));
public string Alias
{
get { return (string)GetValue(Alias); }
set { SetValue(Alias, value); }
}
private static void AliasPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine($"New Value of Alias: {e.NewValue}");
}
See: Dependency properties overview (WPF .NET)
I have the following class:
public class dm_fourvalues : DependencyObject
{
[JsonProperty]
public double First
{
get { return (double)GetValue(FirstProperty); }
set
{
SetValue(FirstProperty, value);
}
}
public static readonly DependencyProperty FirstProperty =
DependencyProperty.Register("First", typeof(double), typeof(dm_fourvalues),
new FrameworkPropertyMetadata(1d,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(OnFirstPropertyChanged)));
private static void OnFirstPropertyChanged(DependencyObject sender,
DependencyPropertyChangedEventArgs e)
{
dm_fourvalues uci = sender as dm_fourvalues;
if (uci != null)
{
uci.OnFirstChanged();
}
}
private void OnFirstChanged()
{
Console.WriteLine("first on dm fourvalues changed");
}
Now if use this class as identical property on another object, when the First property gets changed,
this object's OnChanged is not triggered, thus also a binding would not work.
What defines that a parent dependencyobject has been changed?
I've defined an event in a custom control (Field) named ValueChanged.
public static event EventHandler<ValueChangedEventArgs> ValueChanged;
And a dependency property Value.
public string Value
{
get => (string)GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(nameof(Value), typeof(string), typeof(Field),
new PropertyMetadata(OnValuePropertyChanged));
I need to fire my event when this value changes (if FireValueChanged is true).
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
bool fire = (bool)d.GetValue(FireValueChangedProperty);
if (fire) ValueChanged?.Invoke(d, new ValueChangedEventArgs($"{e.NewValue}", $"{e.OldValue}"));
}
This is the ValueChangedEventArgs class
public class ValueChangedEventArgs : EventArgs
{
public string NewValue { get; }
public string OldValue { get; }
//Other calculated properties...
public ValueChangedEventArgs(string newValue, string oldValue)
{
NewValue = newValue;
OldValue = oldValue;
}
}
But in my main window it says that
cannot set the handler because the event is a static event.
And when I try to compile it says that
the property 'ValueChanged' does not exist in the XML namespace 'clr-namespace: ...'.
If I try to set the event as non static, I cannot use it inside static OnValuePropertyChanged method.
You can access the control that the value was changed for in your OnValuePropertyChanged method like this (I've named the control class MyControl):
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
bool fire = (bool)d.GetValue(FireValueChangedProperty);
var ctrl = (MyControl)d;
if (fire)
ctrl.ValueChanged?.Invoke(d, new ValueChangedEventArgs($"{e.NewValue}", $"{e.OldValue}"));
}
Then you can remove the static and change the event to be an event on instance level:
public event EventHandler<ValueChangedEventArgs> ValueChanged;
Suppose we have a control with a dependency property:
public class MyControl : ContentControl
{
static MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
}
void DoSomething(object sender, RoutedEventArgs e)
{
//...
}
public MyControl()
{
this.Loaded += DoSomething;
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(string), typeof(MyControl),
new FrameworkPropertyMetadata(default(string),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, OnValueChanged));
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//some actions
}
public string Value
{
get { return (string)GetValue(MyControl.ValueProperty); }
set { SetValue(MyControl.ValueProperty, value); }
}}
And somewhere in XAML we bind ValueProperty to some property Val of some ViewModel.
My question is, at the moment when Loaded event is fired, will the value of MyControl.Value be already set to the value of Val which it has or not? So, am I able to use Val in DoSomething method which is executed when MyControl is loaded or not?
Thank you in advance for any answers.
My question is, at the moment when Loaded event is fired, will the value of MyControl.Value be already set to the value of Val which it has or not?
You cannot rely on this. The Loaded event is not a 'data-binding-has-completed' event. Also, the value of a dependency property can change at any time and the place to handle any changes to your Value property is in the OnValueChanged callback. You could check whether the control has been loaded in the callback if you want to:
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyControl ctrl = d as MyControl;
if (ctrl.IsLoaded)
{
//...
}
}
The point is that you should perform any action that depends upon the value of your dependency property in the callback and not in the Loaded event handler.
public class MyControl : ContentControl
{
public MyControl()
{
this.Loaded += (s,e) => DoSomething();
}
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var c = (MyControl)d;
if ( c.IsLoaded )
c.DoSomething();
}
// this will be called only if the control is loaded
private void DoSomething()
{
var value = Value;
if ( value == null )
{
...
} else if ( value == string.Empty )
{
...
} else
{
}
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(string), typeof(MyControl),
new FrameworkPropertyMetadata(default(string),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, OnValueChanged));
public string Value
{
get { return (string)GetValue(MyControl.ValueProperty); }
set { SetValue(MyControl.ValueProperty, value); }
}
static MyControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
}
}
Is there any way to listen to changes of a DependencyProperty? I want to be notified and perform some actions when the value changes but I cannot use binding. It is a DependencyProperty of another class.
This method is definitely missing here:
DependencyPropertyDescriptor
.FromProperty(RadioButton.IsCheckedProperty, typeof(RadioButton))
.AddValueChanged(radioButton, (s,e) => { /* ... */ });
Caution: Because DependencyPropertyDescriptor has a static list of all handlers in application every object referenced in those handlers will leak if the handler is not eventually removed. (It does not work like common events on instance objects.)
Always remove a handler again using descriptor.RemoveValueChanged(...).
If it's a DependencyProperty of a separate class, the easiest way is to bind a value to it, and listen to changes on that value.
If the DP is one you're implementing in your own class, then you can register a PropertyChangedCallback when you create the DependencyProperty. You can use this to listen to changes of the property.
If you're working with a subclass, you can use OverrideMetadata to add your own PropertyChangedCallback to the DP that will get called instead of any original one.
I wrote this utility class:
It gives DependencyPropertyChangedEventArgs with old & new value.
The source is stored in a weak reference in the binding.
Not sure if exposing Binding & BindingExpression is a good idea.
No leaks.
using System;
using System.Collections.Concurrent;
using System.Windows;
using System.Windows.Data;
public sealed class DependencyPropertyListener : DependencyObject, IDisposable
{
private static readonly ConcurrentDictionary<DependencyProperty, PropertyPath> Cache = new ConcurrentDictionary<DependencyProperty, PropertyPath>();
private static readonly DependencyProperty ProxyProperty = DependencyProperty.Register(
"Proxy",
typeof(object),
typeof(DependencyPropertyListener),
new PropertyMetadata(null, OnSourceChanged));
private readonly Action<DependencyPropertyChangedEventArgs> onChanged;
private bool disposed;
public DependencyPropertyListener(
DependencyObject source,
DependencyProperty property,
Action<DependencyPropertyChangedEventArgs> onChanged = null)
: this(source, Cache.GetOrAdd(property, x => new PropertyPath(x)), onChanged)
{
}
public DependencyPropertyListener(
DependencyObject source,
PropertyPath property,
Action<DependencyPropertyChangedEventArgs> onChanged)
{
this.Binding = new Binding
{
Source = source,
Path = property,
Mode = BindingMode.OneWay,
};
this.BindingExpression = (BindingExpression)BindingOperations.SetBinding(this, ProxyProperty, this.Binding);
this.onChanged = onChanged;
}
public event EventHandler<DependencyPropertyChangedEventArgs> Changed;
public BindingExpression BindingExpression { get; }
public Binding Binding { get; }
public DependencyObject Source => (DependencyObject)this.Binding.Source;
public void Dispose()
{
if (this.disposed)
{
return;
}
this.disposed = true;
BindingOperations.ClearBinding(this, ProxyProperty);
}
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var listener = (DependencyPropertyListener)d;
if (listener.disposed)
{
return;
}
listener.onChanged?.Invoke(e);
listener.OnChanged(e);
}
private void OnChanged(DependencyPropertyChangedEventArgs e)
{
this.Changed?.Invoke(this, e);
}
}
using System;
using System.Windows;
public static class Observe
{
public static IDisposable PropertyChanged(
this DependencyObject source,
DependencyProperty property,
Action<DependencyPropertyChangedEventArgs> onChanged = null)
{
return new DependencyPropertyListener(source, property, onChanged);
}
}
You could inherit the Control you're trying to listen, and then have direct access to:
protected void OnPropertyChanged(string name)
No risk of memory leak.
Don't be afraid of standard OO techniques.
There are multiple ways to achieve this. Here is a way to convert a dependent property to an observable, such that it can be subscribed to using System.Reactive:
public static class DependencyObjectExtensions
{
public static IObservable<EventArgs> Observe<T>(this T component, DependencyProperty dependencyProperty)
where T:DependencyObject
{
return Observable.Create<EventArgs>(observer =>
{
EventHandler update = (sender, args) => observer.OnNext(args);
var property = DependencyPropertyDescriptor.FromProperty(dependencyProperty, typeof(T));
property.AddValueChanged(component, update);
return Disposable.Create(() => property.RemoveValueChanged(component, update));
});
}
}
Usage
Remember to dispose the subscriptions to prevent memory leaks:
public partial sealed class MyControl : UserControl, IDisposable
{
public MyControl()
{
InitializeComponent();
// this is the interesting part
var subscription = this.Observe(MyProperty)
.Subscribe(args => { /* ... */}));
// the rest of the class is infrastructure for proper disposing
Subscriptions.Add(subscription);
Dispatcher.ShutdownStarted += DispatcherOnShutdownStarted;
}
private IList<IDisposable> Subscriptions { get; } = new List<IDisposable>();
private void DispatcherOnShutdownStarted(object sender, EventArgs eventArgs)
{
Dispose();
}
Dispose(){
Dispose(true);
}
~MyClass(){
Dispose(false);
}
bool _isDisposed;
void Dispose(bool isDisposing)
{
if(_disposed) return;
foreach(var subscription in Subscriptions)
{
subscription?.Dispose();
}
_isDisposed = true;
if(isDisposing) GC.SupressFinalize(this);
}
}
If that is the case, One hack. You could introduce a Static class with a DependencyProperty. You source class also binds to that dp and your destination class also binds to the DP.