I have the following WPF code. You can see in the comments there that I have a problem with my OnValueChanged handler. I need the code there to differentiate between a Value set from the UI (through various bindings) and one set from the manager class. I had hoped that DependencyPropertyChangedEventArgs would have some kind of source that I could use to differentiate this, but I don't see anything like that. Ideas? Is there some way to set a WPF DependencyProperty without triggering its PropertyChanged handler? Thanks for your time.
public class GaugeBaseControl : UserControl
{
protected readonly AssetModelManager Manager;
public GaugeBaseControl(AssetModelManager mgr)
{
Manager = mgr;
if(mgr != null)
mgr.TelemetryValueChanged += MgrOnTelemetryValueChanged; // coming on background thread
}
private void MgrOnTelemetryValueChanged(KeyValuePair<string, object> keyValuePair)
{
if(_localTelemetryId != keyValuePair.Key)
return;
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
if (!Equals(Value, keyValuePair.Value))
Value = keyValuePair.Value;
}));
}
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var gbc = (GaugeBaseControl) d;
var id = gbc.TelemetryId;
if (!string.IsNullOrEmpty(id))
{
// this is the problem:
// I need to always set gbc.Manager[id] if this event was triggered from the UI (even when equal)
// however, if it was triggered by TelemetryValueChanged then we don't want to go around in circles
if (!Equals(gbc.Manager[id], e.NewValue))
gbc.Manager[id] = e.NewValue;
}
}
private string _localTelemetryId; // to save us a cross-thread check
private static void OnTelemetryIdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var gbc = (GaugeBaseControl)d;
var tid = gbc.TelemetryId;
gbc._localTelemetryId = tid;
gbc.Value = string.IsNullOrEmpty(tid) ? null : gbc.Manager[tid];
}
public static readonly DependencyProperty TelmetryIdProperty = DependencyProperty.Register("TelemetryId", typeof(string), typeof(GaugeBaseControl), new PropertyMetadata(OnTelemetryIdChanged));
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(object), typeof(GaugeBaseControl), new PropertyMetadata(OnValueChanged));
public object Value
{
get { return GetValue(ValueProperty); }
set { SetValue(ValueProperty, value);}
}
public string TelemetryId
{
get { return (string)GetValue(TelmetryIdProperty); }
set { SetValue(TelmetryIdProperty, value); }
}
}
It seems a bit hackish, but it is the best shot i could come up with without changing the architecture. You could stop listening on the TelemetryValueChanged event while doing your internal update to stop the roundtrip like so:
internal void SetManagerIdInternal(string id, object value)
{
if(mgr != null)
{
mgr.TelemetryValueChanged -= MgrOnTelemetryValueChanged;
mgr[id] = value;
mgr.TelemetryValueChanged += MgrOnTelemetryValueChanged;
}
}
And use it like this:
if (!Equals(gbc.Manager[id], e.NewValue))
SetManagerIdInternal(id, e.NewValue);
You could also use a private field to just skip doing work without unregistering/reregistering the event in MgrOnTelemetryValueChanged wich might be better performance wise, but i haven't tested it.
Related
I'm doing some training project right now. It's supposed to convert numbers into different strings.
Heres the converted Control, and in the bottom way I use it in my Main Window.
So the first problem is that I want to create instance of converter based on value I pass to OutputFormatProperty so in this case I create converter that should be type OctalConverter but instead I get the default one, why is that?
Another thing is that I wan't to change InputValue in the converter by binding it to CurrentValue, which works with NotifyPropertyChanged, but it doesn't seem to work that way.
public partial class ConverterDisplay : UserControl {
private const int DEFAULT_INPUT_VALUE = 0;
private readonly ObservableCollection <DisplayField> _displayFields;
private AbstractNumberConverter _converter;
public static readonly DependencyProperty InputValueProperty = DependencyProperty.Register (
"InputValue",
typeof(int),
typeof(ConverterDisplay),
new PropertyMetadata (DEFAULT_INPUT_VALUE));
public static readonly DependencyProperty OutputFormatProperty = DependencyProperty.Register (
"OutputFormat",
typeof(NumberSystems),
typeof(ConverterDisplay),
new FrameworkPropertyMetadata (NumberSystems.Binary));
public int InputValue {
get {
return (int) GetValue (InputValueProperty);
}
set {
SetValue (InputValueProperty, value);
UpdateDisplay ();
}
}
public NumberSystems OutputFormat {
get {
return (NumberSystems) GetValue (OutputFormatProperty);
}
set {
SetValue (OutputFormatProperty, value);
}
}
public ObservableCollection <DisplayField> DisplayFields {
get { return _displayFields; }
}
public ConverterDisplay () {
_displayFields = new ObservableCollection<DisplayField> ();
InitializeComponent ();
CreateConverter ();
}
private void UpdateDisplay () {
var convertedNumberString = _converter.GetString (InputValue);
if (_displayFields.Count > convertedNumberString.Length)
ResetDisplayFields ();
while (_displayFields.Count < convertedNumberString.Length)
AddDisplayField ();
UpdateValues (convertedNumberString);
}
private void UpdateValues (string convertedString) {
if (_displayFields.Count == 0) return;
for (int i = 0; i < _displayFields.Count; i++) {
_displayFields [i].NumberValue = convertedString [i];
}
}
private void AddDisplayField () {
_displayFields.Insert (
0,
new DisplayField ((int)OutputFormat, _displayFields.Count));
}
private void ResetDisplayFields () {
_displayFields.Clear ();
}
private void CreateConverter () {
switch (OutputFormat) {
case NumberSystems.Binary:
_converter = new BinaryConverter ();
break;
case NumberSystems.Octal:
_converter = new OctalConverter ();
break;
case NumberSystems.Hexadecimal:
_converter = new HexadecimalConverter ();
break;
}
}
}
public enum NumberSystems {
Binary = 2,
Octal = 8,
Hexadecimal = 16
}
And then in the Main Window I'm trying to use that control
<converters:ConverterDisplay x:Name="octConverter"
InputValue="{Binding ElementName=Window,Path=CurrentValue}"
OutputFormat="Octal"/>
Just in case
public int CurrentValue {
get { return _currentValue; }
set {
if (value == _currentValue)
return;
ValidateNewValue (value);
OnPropertyChanged ();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged ([CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
}
===========================
Edit #1
I don't really like that solution but I created public method in ConverterDisplay to create converter, it's being called after MainWindow is initialized so now the converters are correct.
Another thing is that how do i bind my UpdateDisplay method to InputValueProperty? I found through validation that it's getting correct value, but I can't see way how I can run that method without creating static stuff.
Concerning your second problem (binding the UpdateDisplay method to InputValueProperty: In general, it's not the best idea to call any method within a dependency property's setter, since this setter is never invoked when using data binding to fill the dependency property's value, as pointed out at MSDN:
The WPF XAML processor uses property system methods for dependency
properties when loading binary XAML and processing attributes that are
dependency properties. This effectively bypasses the property
wrappers. When you implement custom dependency properties, you must
account for this behavior and should avoid placing any other code in
your property wrapper other than the property system methods GetValue
and SetValue.
Instead, create a callback method that is invoked whenever InputValue's content changes, and call UpdateDisplay from there:
public static readonly DependencyProperty InputValueProperty = DependencyProperty.Register (
"InputValue",
typeof(int),
typeof(ConverterDisplay),
new PropertyMetadata (DEFAULT_INPUT_VALUE, InputValueChangedCallback));
private static void InputValueChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
var userControl = dependencyObject as ConverterDisplay;
if (userControl != null)
userControl.UpdateDisplay();
}
I have created a custom TextEditor control that inherits from AvalonEdit. I have done this to facilitate the use of MVVM and Caliburn Micro using this editor control. The [cut down for display purposes] MvvTextEditor class is
public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
public MvvmTextEditor()
{
TextArea.SelectionChanged += TextArea_SelectionChanged;
}
void TextArea_SelectionChanged(object sender, EventArgs e)
{
this.SelectionStart = SelectionStart;
this.SelectionLength = SelectionLength;
}
public static readonly DependencyProperty SelectionLengthProperty =
DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.SelectionLength = (int)args.NewValue;
}));
public new int SelectionLength
{
get { return base.SelectionLength; }
set { SetValue(SelectionLengthProperty, value); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] string caller = null)
{
var handler = PropertyChanged;
if (handler != null)
PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
}
Now, in the view that holds this control, I have the following XAML:
<Controls:MvvmTextEditor
Caliburn:Message.Attach="[Event TextChanged] = [Action DocumentChanged()]"
TextLocation="{Binding TextLocation, Mode=TwoWay}"
SyntaxHighlighting="{Binding HighlightingDefinition}"
SelectionLength="{Binding SelectionLength,
Mode=TwoWay,
NotifyOnSourceUpdated=True,
NotifyOnTargetUpdated=True}"
Document="{Binding Document, Mode=TwoWay}"/>
My issue is SelectionLength (and SelectionStart but let us just consider the length for now as the problem is the same). If I selected something with the mouse, the binding from the View to my View Model works great. Now, I have written a find and replace utility and I want to set the SelectionLength (which has get and set available in the TextEditor control) from the code behind. In my View Model I am simply setting SelectionLength = 50, I implement this in the View Model like
private int selectionLength;
public int SelectionLength
{
get { return selectionLength; }
set
{
if (selectionLength == value)
return;
selectionLength = value;
Console.WriteLine(String.Format("Selection Length = {0}", selectionLength));
NotifyOfPropertyChange(() => SelectionLength);
}
}
when I set SelectionLength = 50, the DependencyProperty SelectionLengthProperty does not get updated in the MvvmTextEditor class, it is like the TwoWay binding to my control is failing but using Snoop there is no sign of this. I thought this would just work via the binding, but this does not seem to be the case.
Is there something simple I am missing, or will I have to set up and event handler in the MvvmTextEditor class which listens for changes in my View Model and updated the DP itself [which presents it's own problems]?
Thanks for your time.
This is because the Getter and Setter from a DependencyProperty is only a .NET Wrapper. The Framework will use the GetValue and SetValue itself.
What you can try is to access the PropertyChangedCallback from your DependencyProperty and there set the correct Value.
public int SelectionLength
{
get { return (int)GetValue(SelectionLengthProperty); }
set { SetValue(SelectionLengthProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectionLength. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectionLengthProperty =
DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor), new PropertyMetadata(0,SelectionLengthPropertyChanged));
private static void SelectionLengthPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var textEditor = obj as MvvmTextEditor;
textEditor.SelectionLength = e.NewValue;
}
Here is another answer if you are still open. Since SelectionLength is already defined as a dependency property on the base class, rather than create a derived class (or add an already existing property to the derived class), I would use an attached property to achieve the same functionality.
The key is to use System.ComponentModel.DependencyPropertyDescriptor to subscribe to the change event of the already existing SelectionLength dependency property and then take your desired action in the event handler.
Sample code below:
public class SomeBehavior
{
public static readonly DependencyProperty IsEnabledProperty
= DependencyProperty.RegisterAttached("IsEnabled",
typeof(bool), typeof(SomeBehavior), new PropertyMetadata(OnIsEnabledChanged));
public static void SetIsEnabled(DependencyObject dpo, bool value)
{
dpo.SetValue(IsEnabledProperty, value);
}
public static bool GetIsEnabled(DependencyObject dpo)
{
return (bool)dpo.GetValue(IsEnabledProperty);
}
private static void OnIsEnabledChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
{
var editor = dpo as TextEditor;
if (editor == null)
return;
var dpDescriptor = System.ComponentModel.DependencyPropertyDescriptor.FromProperty(TextEditor.SelectionLengthProperty,editor.GetType());
dpDescriptor.AddValueChanged(editor, OnSelectionLengthChanged);
}
private static void OnSelectionLengthChanged(object sender, EventArgs e)
{
var editor = (TextEditor)sender;
editor.Select(editor.SelectionStart, editor.SelectionLength);
}
}
Xaml below:
<Controls:TextEditor Behaviors:SomeBehavior.IsEnabled="True">
</Controls:TextEditor>
This is how I did this...
public static readonly DependencyProperty SelectionLengthProperty =
DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
if (target.SelectionLength != (int)args.NewValue)
{
target.SelectionLength = (int)args.NewValue;
target.Select(target.SelectionStart, (int)args.NewValue);
}
}));
public new int SelectionLength
{
get { return base.SelectionLength; }
//get { return (int)GetValue(SelectionLengthProperty); }
set { SetValue(SelectionLengthProperty, value); }
}
Sorry for any time wasted. I hope this helps someone else...
I have a ListBox in WPB bound to an ObservableCollection
public static readonly DependencyProperty ProgramsProperty =
DependencyProperty.Register("Programs",
typeof(ObservableCollection<ProgramData>), typeof(ProgramView),
new PropertyMetadata(default(ObservableCollection<ProgramData>)));
public ObservableCollection<ProgramData> Programs
{
get { return (ObservableCollection<ProgramData>)GetValue(ProgramsProperty); }
set { SetValue(ProgramsProperty, value); }
}
and the selected element of the ListBox bound to one element
public static readonly DependencyProperty SelectedProgramProperty =
DependencyProperty.Register("SelectedProgram",
typeof(ProgramData), typeof(ProgramView),
new PropertyMetadata(default(ProgramData)));
public DispenserInfo SelectedProgram
{
get { return (ProgramData)GetValue(SelectedProgramProperty); }
set { SetValue(SelectedProgramProperty, value); }
}
When the user changes the selected element in the ListBox I would like to check the status of the "old" selected element - maybe we need to save the old element - and react in some way.
So I would like to something like this
public static bool UpdateCallback(ProgramData oldVal, ProgramData newVal)
{
if (oldVal.DataChanged == false)
return true;
var res = MessageBox.Show("Save or discard changes", "Question",
MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
switch (res)
{
case MessageBoxResult.Yes:
oldVal.Save();
return true;
case MessageBoxResult.No:
oldVal.Discard();
return true;
case MessageBoxResult.Cancel:
return false;
}
return true;
}
I there a way to do this with validation/coercing callbacks?
Update:
As Oliver and Sheridan suggested I tried the changed as well as the coerce callback and this is working quite well to do some tasks in between clicking and refreshing the UI. But when I try to cancel the update in the coerceCallback like this
private static object CoerceValueCallback(DependencyObject dependencyObject, object baseValue)
{
var #this = (ProgramView)dependencyObject;
var res = MessageBox.Show("Save or discard changes", "Question", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
switch (res)
{
case MessageBoxResult.Cancel:
return #this.SelectedProgram;
}
return baseValue;
}
the UI stays at the old values as expected but the ListBox highlights the wrong line - the one the user has clicked and not the one from the binding when canceled in the coerceCallback.
Do I need to update the binding manually? Any ideas
You can add a PropertyChanged handler to your DependencyProperty:
public static readonly DependencyProperty ProgramsProperty =
DependencyProperty.Register("Programs",
typeof(ObservableCollection<ProgramData>), typeof(ProgramView),
new UIPropertyMetadata(default(ObservableCollection<ProgramData>,
(d, e) => ((ProgramView)d).OnProgramsChanged(d, e))));
private void OnProgramsChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
// Do something with e.OldValue and e.NewValue here
}
Sure there is. Look at the last parameter of DependencyProperty.Register !
public static readonly DependencyProperty MyPropertyProperty =
System.Windows.DependencyProperty.Register("MyProperty", typeof(string), typeof(MyClass), new PropertyMetadata(default(string), (
sender, args) =>
{
//changed callback
var #this = (MyClass)sender;
var newval = (string)args.NewValue;
var oldval = (string)args.NewValue;
// do whatever you like
},
(s, e) =>
{
// coerce callback
if (e == null)
return " - empty string -";
return e;
}));
For most WPF dependency properties, there is even more: Properties metadata are an instance of FrameworkPropertyMetadata instead of simple good old PropertyMetadata. It has more "hidden" options. You may use it as well instead of PropertyMetadata.
I have a class inheriting canvas with the following dependency property
public class StorageCanvas : Canvas
{
public readonly static DependencyProperty StorageProperty = DependencyProperty.Register(
"Storage",
typeof(Polygon),
typeof(StorageCanvas));
public Polygon Storage
{
get { return (Polygon) GetValue(StorageProperty); }
set { SetValue(StorageProperty, value); }
}
}
Can I somehow make the dependency property "update" when the Storage polygon Points has been altered/updated, instead of requiring the polygon to be replaced with a new instance?
Well Polygon.Points is a PointCollection, so you could just subscribe to the Changed event of it and then call InvalidateVisual() as suggested by #dowhilefor
public class StorageCanvas : Canvas {
public static readonly DependencyProperty StorageProperty = DependencyProperty.Register(
"Storage",
typeof(Polygon),
typeof(StorageCanvas),
new FrameworkPropertyMetadata(null, PropertyChangedCallback));
public Polygon Storage {
get {
return (Polygon)GetValue(StorageProperty);
}
set {
SetValue(StorageProperty, value);
}
}
private static void PropertyChangedCallback(
DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) {
var currentStorageCanvas = dependencyObject as StorageCanvas;
if (currentStorageCanvas == null)
return;
var oldPolygon = args.OldValue as Polygon;
if (oldPolygon != null)
oldPolygon.Points.Changed -= currentStorageCanvas.PointsOnChanged;
var newPolygon = args.NewValue as Polygon;
if (newPolygon == null)
return;
newPolygon.Points.Changed += currentStorageCanvas.PointsOnChanged;
// Just adding the following to test if updates are fine.
currentStorageCanvas.Children.Clear();
currentStorageCanvas.Children.Add(newPolygon);
}
private void PointsOnChanged(object sender, EventArgs eventArgs) {
InvalidateVisual();
}
}
So now if any individual Point in Storage changed, without actually recreating the entire object, InvalidateVisual() will be fired.
The concept is just about subscribing to the Changed event of PointsCollection. Whether it's the right thing to do for you is a question you need to address yourself based on your requirements and logic.
Register the dependency property with Affects render meta data option.
http://msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadata.affectsrender.aspx
If I have XAML element that is binded to a data source property and the data source changes faster then the human eye can see I am assuming that the UI also gets re drawn faster then the human eye can see and wasting resources. Would it be a good idea for the property change to raise a flag instead of triggering re-draw of UI and then a timer to trigger the UI redraw if the property was changed? or am I missing how the UI gets re-drawn?
You Could use a delyed invocation of raising the property changed event, maybe like this...
public static class DispatcherExtensions
{
private static Dictionary<string, DispatcherTimer> timers =
new Dictionary<string, DispatcherTimer>();
private static readonly object syncRoot = new object();
public static void DelayInvoke(this Dispatcher dispatcher, string namedInvocation,
Action action, TimeSpan delay,
DispatcherPriority priority = DispatcherPriority.Normal)
{
lock (syncRoot)
{
RemoveTimer(namedInvocation);
var timer = new DispatcherTimer(delay, priority, (s, e) => action(), dispatcher);
timer.Start();
timers.Add(namedInvocation, timer);
}
}
public static void CancelNamedInvocation(this Dispatcher dispatcher, string namedInvocation)
{
lock (syncRoot)
{
RemoveTimer(namedInvocation);
}
}
private static void RemoveTimer(string namedInvocation)
{
if (!timers.ContainsKey(namedInvocation)) return;
timers[namedInvocation].Stop();
timers.Remove(namedInvocation);
}
}
Then
private object _property;
public object Property
{
get { return _property; }
set
{
if (_property != value)
{
_property = value;
Dispatcher.DelayInvoke("PropertyChanged_Property",(Action)(() =>
{
RaisePropertyChanged("Property");
}),TimeSpan.FromMilliseconds(500));
}
}
}
Not sure i like it though...
A typical pattern for this case, is implementing your property as
private object _property;
public object Property
{
get { return _property; }
set
{
if (_property != value)
{
_property = value;
RaisePropertyChanged("Property");
}
}
}
which will update your binding only if the value changed
In this case a timer triggering the UI update is the way to go. To keep the UI fluent take an timer interval of about 40ms.
public class ViewModel
{
private Timer updateTimer;
public ViewModel()
{
updateTimer = new Timer();
updateTimer.Interval = 40;
updateTimer.Elapsed +=new ElapsedEventHandler(updateTimer_Elapsed);
updateTimer.Start();
}
private object _property;
public object Property
{
get { return _property; }
set
{
if (_property != value)
{
_property = value;
}
}
}
void updateTimer_Elapsed(object sender, ElapsedEventArgs e)
{
RaisePropertyChanged();
}
}
Calling RaisePropertyChanged() without an argument forces the UI to refresh all bindings. If you don't want this, you could use flags or a registry, to mark which property needs to be updated.