DependencyProperty default value depends on coercion logic - c#

I have this simple example in the ViewModel of a WPF application:
class VM_DiskPartition : DependencyObject
{
// (...) Other properties
public bool IsLowOnSpace
{
get { return (bool)GetValue(IsLowOnSpaceProperty); }
set { SetValue(IsLowOnSpaceProperty, value); }
}
public static readonly DependencyProperty IsLowOnSpaceProperty = DependencyProperty.Register("IsLowOnSpace", typeof(bool), typeof(VM_DiskPartition), new PropertyMetadata(false, OnLowOnSpaceChanged));
private static void OnLowOnSpaceChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
{
((VM_DiskPartition)d).CoerceValue(BgColorProperty);
}
public Brush BgColor
{
get { return (Brush)GetValue(BgColorProperty); }
set { SetValue(BgColorProperty, value); }
}
public static readonly DependencyProperty BgColorProperty = DependencyProperty.Register("BgColor", typeof(Brush), typeof(VM_DiskPartition), new PropertyMetadata(Brushes.Red, null, Coerce_BgColor));
private static object Coerce_BgColor(DependencyObject d, object baseValue)
{
return UIUtils.GetBgColor(((VM_DiskPartition)d).IsLowOnSpace);
}
}
I want the BgColor property to have its default value automatically set by its coercion function.
Is there a more elegant way to achieve this instead of calling CoerceValue(BgColorProperty) from the constructor?
The reason is that I may have many properties like this in the future and it doesn't look very clean to use a lot of CoerceValue() calls in the constructor.
Maybe it's better to use Converters in this scenario? I was trying to go without them and create new ViewModel properties instead.

You seem to be somewhat confused... the DependencyObject and DependencyProperty classes are UI classes. They don't belong in a view model. In view models, we use normal CLR properties and the INotifyPropertyChanged interface to handle property change notification. Therefore, there's no need to use them in a view model at all.
If you want to set a default value in a view model, you simply do this:
private int number = 5; // <-- default value
public int Number
{
get { return number; }
set { number = value; NotifyPropertyChanged("Number"); }
}
If you want property value coercion in a view model, you just do this:
public int Number
{
get { return number; }
set { number = Math.Max(0, value); NotifyPropertyChanged("Number"); }
}
UPDATE >>>
Looking again at your code, it occurs to me that it shouldn't be in a view model at all. It looks like it should be in the code behind of some UserControl. We put data in view models, not UI elements like Brushes. If you want to set a default value for a DependencyProperty, the correct way to do it is how you have shown us:
public static readonly DependencyProperty BgColorProperty =
DependencyProperty.Register("BgColor", typeof(Brush), typeof(VM_DiskPartition),
new PropertyMetadata(Brushes.Red/* <-- default value */, null, Coerce_BgColor));
Property coercion is for ensuring that a value stays within certain bounds like the example I gave above that ensures that the value will never be negative.

Related

DependencyProperty not update [duplicate]

When I set the value of IsClosed during runtime, OnIsClosedChanged() is called fine.
However, the Designer sets the value of the property but does not call the OnIsClosedChanged().
public static DependencyProperty IsClosedProperty = DependencyProperty.Register("IsClosed", typeof(bool), typeof(GroupBox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
public bool IsClosed {
get {
return (bool)this.GetValue(IsClosedProperty);
}
set {
if ((bool)this.GetValue(IsClosedProperty) == value)
return;
this.SetValue(IsClosedProperty, value);
OnIsClosedChanged();
}
}
private void OnIsClosedChanged() {
_rowDefContent.Height = new GridLength((IsClosed ? 0 : 1), GridUnitType.Star);
}
Obviously IsClosed is not modified by the Designer and only IsClosedProperty receives the xaml change.
My question is: How can I run IsClosed after the value has been modified in the Designer. Or at least add some logic to the non-runtime changes.
You would have to register a PropertyChangedCallback with property metadata.
The reason is that dependency properties set in XAML or by bindings or some other source do not invoke the CLR wrapper (the setter method). The reason is explained in the XAML Loading and Dependency Properties article on MSDN:
For implementation reasons, it is computationally less expensive to
identify a property as a dependency property and access the property
system SetValue method to set it, rather than using the property
wrapper and its setter.
...
Because the current WPF implementation of the XAML processor behavior
for property setting bypasses the wrappers entirely, you should not
put any additional logic into the set definitions of the wrapper for
your custom dependency property. If you put such logic in the set
definition, then the logic will not be executed when the property is
set in XAML rather than in code.
Your code should look like this:
public static readonly DependencyProperty IsClosedProperty =
DependencyProperty.Register(
"IsClosed", typeof(bool), typeof(GroupBox),
new FrameworkPropertyMetadata(false,
FrameworkPropertyMetadataOptions.AffectsRender,
(o, e) => ((GroupBox)o).OnIsClosedChanged()));
public bool IsClosed
{
get { return (bool)GetValue(IsClosedProperty); }
set { SetValue(IsClosedProperty, value); }
}
private void OnIsClosedChanged()
{
_rowDefContent.Height = new GridLength((IsClosed ? 0 : 1), GridUnitType.Star);
}
Found the answer myself now. ValidateValueCallback comes really close! (as Alex K has pointed out) But it is a static method and I don't get any reference to the instance which has been changed. The key is to use a PropertyChangedCallback in FrameworkPropertyMetadata which is also an argument passed to the Property.Register method.
See:
public static DependencyProperty IsClosedProperty = DependencyProperty.Register("IsClosed", typeof(bool), typeof(GroupBox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnIsClosedChangedPCC)));
public bool IsClosed {
get {
return (bool)this.GetValue(IsClosedProperty);
}
set {
this.SetValue(IsClosedProperty, value);
OnIsClosedChanged();
}
}
private static void OnIsClosedChangedPCC(DependencyObject d, DependencyPropertyChangedEventArgs e) {
GroupBox current = (GroupBox)d;
current.IsClosed = current.IsClosed;
}
private void OnIsClosedChanged() {
_rowDefContent.Height = new GridLength((IsClosed ? 0 : 1), GridUnitType.Star);
}
That does now re-set the IsClosedValue which triggers the OnIsClosedChanged to run.
Thank's for your help guys!

Detecting changes to a DependencyProperty by my own code

My control has a property that maps to a private variable. When the property is set, I also need to store a certain other variable. When the private variable of the property is set by my own control code, this special handling must not occur. All good.
I now need to refactor that to a DependencyProperty. I only have a change handler here, and all (also internal) accesses to that property must go through the DependencyProperty framework. There is no more private variable that I could set directly. Every change done by my own code now looks exactly the same as changes from external sources like the user of the control or DataBinding.
In my property change handler, how can I determine whether a value change came from my code or somewhere else?
This is the old code:
private DateTime selectedTime;
private int intendedDay;
public DateTime SelectedTime
{
get { return selectedTime; }
set
{
selectedTime = value;
intendedDay = selectedTime.Day;
}
}
In this code, I can set selectedTime directly, not affecting intendedDay which must only be set when SelectedTime is assigned a new value altogether from the outside, or when I see fit in my control.
The DependencyProperty only allows me to detect any changes:
public static DependencyProperty SelectedTimeProperty = DependencyProperty.Register(
"SelectedTime",
typeof(DateTime),
typeof(DateTimeTextBox),
new PropertyMetadata(SelectedTimeChanged));
public DateTime SelectedTime
{
get { return (DateTime) GetValue(SelectedTimeProperty); }
set { SetValue(SelectedTimeProperty, value); }
}
private static void SelectedTimeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
DateTimeTextBox control = (DateTimeTextBox) obj;
// This must not happen where I previously assigned the private variable:
control.intendedDay = control.SelectedTime.Day;
}
private int intendedDay;
So when I would previously assign the private variable, I can now only set the dependency property, which will also change intendedDay.
Why not wrap your dependency property in a normal property? Setting the property will still set the dependency property and setting the private one will not set the intendedDay value.
private DateTime selectedTime
{
get { return (DateTime) GetValue(SelectedTimeProperty); }
set { SetValue(SelectedTimeProperty, value); }
}
public DataTime SelectedTime
{
get { return selectedTime; }
set
{
selectedTime = value;
intendedDay = selectedTime.Day;
}
}
Like Clemens explained in his comment, I added a private variable that indicates whether I am currently updating that property from my own code. I set it to true whenever I want to suppress external value change handling, and false afterwards. So I can distinguish whether it was an internal or external value change.

Bridge DependencyProperty from two binding sources

Consider this scenario, using MVVM:
On my ModelView, I have one property, of type "string", it does notify the change of properties through INotifyPropertyChanged.
In the view, there is (or not) one control, with a DependencyProperty "Notification" of a type which is not a string. That control may or may not change that property depending on facts that only the control knows (neither the ModelView or the View knows about those). That control might even be on other View which may or may not be on the current visual tree.
In the View, I need a bridge between that control's DependencyProperty and the ViewModel's property, so that changing the view property makes the control change its property, and changing the control's DependencyProperty makes the viewmodel's property change its value.
I've got it to work, but I don't think it's an elegant solution. I might be thinking fuzzy these days so I'm asking if there's something obvious that I might have missed.
The obvious way would be either having the ViewModel Property be a DependencyProperty (so it could be bound two ways), however that is not possible right now (plus, it'd break the MVVM pattern, adding view-specific implementations to the viewmodel).
The other obvious way would be binding the Control's DependencyProperty to the ViewModel's property: this works, but just for one view... several properties cannot (or, I don't know how to do it) be bound to the same DependencyProperty: when I set one binding, I lose the other.
Currently this is what I do:
public class BaseViewUserControl : UserControl
{
// Dependency property, bound to the view's property
public string AudioNotification
{
get { return (string)GetValue(AudioNotificationProperty); }
set { SetValue(AudioNotificationProperty, value); }
}
public static readonly DependencyProperty AudioNotificationProperty = DependencyProperty.Register("AudioNotification", typeof(string), typeof(BaseViewUserControl), new FrameworkPropertyMetadata("None", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAudioNotificationPropertyChanged));
// Dependency property, bound to the control's dependency property
public AudioNotificationType AudioNotificationToControl
{
get { return (AudioNotificationType)GetValue(AudioNotificationToControlProperty); }
set { SetValue(AudioNotificationToControlProperty, value); }
}
public static readonly DependencyProperty AudioNotificationToControlProperty = DependencyProperty.Register("AudioNotificationToControl", typeof(AudioNotificationType), typeof(BaseViewUserControl), new FrameworkPropertyMetadata(AudioNotificationType.None, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, OnAudioNotificationToControlCoerceValue));
// Converter
private static IValueConverter _audioNotificationTypeConverter;
private static IValueConverter AudioNotificationTypeConverter
{
get { return _audioNotificationTypeConverter ?? (_audioNotificationTypeConverter = new AudioNotificationConverter()); }
}
private Binding _audioNotificationBinding;
private bool PrepareAudioNotificationControlBinding()
{
if (_audioNotificationBinding != null) return true;
var b = this.FindVisualTreeRoot().TryFindChild<AudioNotification>();
if (b == null) return false;
_audioNotificationBinding = new Binding { Source = b, Mode = BindingMode.TwoWay, Path = new PropertyPath("Notification") };
SetBinding(AudioNotificationToControlProperty, _audioNotificationBinding);
return true;
}
private static void OnAudioNotificationPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
if (!(source is BaseViewUserControl)) return;
var src = (BaseViewUserControl)source;
if(src.PrepareAudioNotificationControlBinding())
{
var val = AudioNotificationTypeConverter.ConvertValue<AudioNotificationType>(e.NewValue);
src.AudioNotificationToControl = val;
}
}
private static object OnAudioNotificationToControlCoerceValue(DependencyObject source, object basevalue)
{
if (!(source is BaseViewUserControl)) return basevalue;
var src = (BaseViewUserControl)source;
var val = AudioNotificationTypeConverter.ConvertBackValue<string>(basevalue);
src.AudioNotification = val;
return basevalue;
}
public BaseViewUserControl()
{
var ab = new Binding { Path = new PropertyPath("AudibleNotification"), Mode = BindingMode.TwoWay };
SetBinding(AudibleNotificationProperty, ab);
}
}
NOTE: I'm using this for several things, not just for audio notification (that's an example only). Do not rely on the names to give a solution (if any), this needs to be quite generic. Also, any typos come from simplifying the code to the problem (I've removed much code and changed some property names for clarification).
As I said, it works... I just find it quite not-elegant and I'm sure there should be a better solution than this.
Any suggestions will be more than welcome.
Update
Based on Julien's code, I made this Behavior, which does exactly what I wanted. I implemented it using Converter, but for clarity's sake, I ended up doing the conversion on the control itself and using strings to pass variables along (with an undocumented property in the control's if I still want to use the native data type)
public class BridgePropertyBinderBehavior : Behavior<DependencyObject>
{
public static BridgePropertyBinderBehavior PrepareBindingToControl(FrameworkElement sourceView, string viewModelPropertyPath, FrameworkElement targetControl, string controlPropertyPath)
{
var b = new BridgePropertyBinderBehavior();
BindingOperations.SetBinding(b, AProperty, new Binding(viewModelPropertyPath) { Source = sourceView.DataContext, Mode = BindingMode.TwoWay, BindsDirectlyToSource = true, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
BindingOperations.SetBinding(b, BProperty, new Binding(controlPropertyPath) { Source = targetControl, Mode = BindingMode.TwoWay });
Interaction.GetBehaviors(sourceView).Add(b);
return b;
}
public object A { get { return GetValue(AProperty); } set { SetValue(AProperty, value); } }
public static readonly DependencyProperty AProperty = DependencyProperty.Register("A", typeof(object), typeof(BridgePropertyBinderBehavior), new FrameworkPropertyMetadata(null, (d, e) => ((BridgePropertyBinderBehavior)d).OnAChanged(e.NewValue)));
public object B { get { return GetValue(BProperty); } set { SetValue(BProperty, value); } }
public static readonly DependencyProperty BProperty = DependencyProperty.Register("B", typeof(object), typeof(BridgePropertyBinderBehavior), new FrameworkPropertyMetadata(null, (d, e) => ((BridgePropertyBinderBehavior)d).OnBChanged(e.NewValue)));
private void OnAChanged(object value) { B = value; }
private void OnBChanged(object value) { A = value; }
protected override Freezable CreateInstanceCore()
{
return new BridgePropertyBinderBehavior();
}
}
Which I use like this on my view:
var audioNotificationControl = this.FindVisualTreeRoot().TryFindChild<AudioNotification>();
BridgePropertyBinderBehavior.PrepareBindingToControl(this, "AudioNotification", audioNotificationControl, "Notification");
or
<AudioNotification x:Name="Control">
<ia:Interaction.Behaviors>
<BridgePropertyBinderBehavior
A="{Binding Path=Notification, ElementName=Control, Mode=TwoWay}"
B="{Binding Path=AudioNotification, Mode=TwoWay}" />
</ia:Interaction.Behaviors>
</AudioNotification>
I've accepted his answer since it's what got me on the right track, thanks
If I understand you correctly, you need to bind one DP to two sources, one as a source and the other as a target. I actually have a behavior to do that.
The principle of this behavior is quite simple: it uses two dependency properties and makes the data of one (In) flows into the other (Out). Bind In with a one way binding and Out with a one way to source binding and you're done.
public class BindingBehavior : Behavior<DependencyObject> {
public static readonly DependencyProperty InProperty = DependencyProperty.Register(
"In",
typeof(object),
typeof(BindingBehavior),
new FrameworkPropertyMetadata(null, (d, e) => ((BindingBehavior) d).OnInPropertyChanged(e.NewValue)));
public static readonly DependencyProperty OutProperty = DependencyProperty.Register(
"Out",
typeof(object),
typeof(BindingBehavior),
new FrameworkPropertyMetadata(null));
// Bind OneWay
public object In {
get { return GetValue(InProperty); }
set { SetValue(InProperty, value); }
}
// Bind OneWayToSource
public object Out {
get { return GetValue(OutProperty); }
set { SetValue(OutProperty, value); }
}
private void OnInPropertyChanged(object value) {
Out = value;
}
protected override Freezable CreateInstanceCore() {
return new BindingBehavior();
}
}
This behavior needs a reference to System.Windows.Interactivity from Blend SDK, which you might be familiar with.
Assuming you remove your string property and only keep a AudioNotificationType one named AudtioNotification, the usage should be similar to:
<YourView x:Name="View">
<YourControl x:Name="Control" AudioNotification="{Binding Notification, ElementName=View}>
<i:Interaction.Behaviors>
<BindingBehavior
In="{Binding AudioNotification, ElementName=Control, Mode=OneWay}"
Out="{Binding YourVmProperty, Mode=OneWayToSource, Converter=YourConverter}" />
</i:Interaction.Behaviors>
</YourControl>
</YourView>
You can place the behavior on any element being on the correct name scope for resolving element names and having the view model as the data context.
This looks like it might be a useful time to add a layer of abstraction. I know. Ick. But bear with me.
What if you had a bridge object that an abitrary number of things can bind to that handles the notifications on change. It doesn't even need to be that complex. Just something that implements INotifyPropertyChanged and then has a property (or properties) that release a notification on change. That way, your ViewModel, your View and your control can all bind to the same property on this bridge object and when one of those changes the bridge object's property, all the others will know that it's time to change as well. As long as all the objects are bound two-way, everything should synch just fine.
That's essentially what you've done on your BaseViewUserControl, but encapsulating the behavior in a separate object might provide you flexibility benefits.

How can I update my usercontrol when I set a value in a dependency object?

This is the part where is not working. My dependency property has a default value which is Entradas.Entero, and that value must be run this line:
Grid.SetColumnSpan(button0, 3);
And it should refresh it in my user control design, however there's no changes in it.
public partial class TableroUserControl : UserControl
{
public enum Entradas
{
Entero, Decimal
}
public Entradas Entrada
{
get { return (Entradas)GetValue(EntradaProperty); }
set { SetValue(EntradaProperty, value); }
}
static void textChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
Button button0 = ((TableroUserControl)property).button0;
switch ((Entradas)args.NewValue)
{
case Entradas.Entero:
Grid.SetColumnSpan(button0, 3);
break;
case Entradas.Decimal:
Grid.SetColumnSpan(button0, 2);
break;
}
}
public static readonly DependencyProperty EntradaProperty =
DependencyProperty.Register("Entrada", typeof(Entradas), typeof(TableroUserControl), new PropertyMetadata(Entradas.Entero, new PropertyChangedCallback(textChangedCallBack)));
public TableroUserControl()
{
InitializeComponent();
}
}
You might also consider doing this with a value converter. You should be able to bind the Grid.ColumnSpan attached property of button0 to the Entrada property of your user control. Then use a value converter to convert it to an integer. This way you don't have to deal with callbacks and state/timing issues.
Your dependency property is initialized to Entero. So unless the value is changed to Decimal and then again changed back to Entero you wont hit the property changed callback code.
Make sure that the colspan setter code is hit.

WPF C# Recording Dependency object data modification

Am working with dependency objects here, not done much with them before but they look very useful.
Basically I've used these objects to be editable in a ListView. However, I then have to write these changes back to SQL. My question is, is there a way to record whether the data has been modified because I don't want to write back to SQL every time someone views the data. Currently I have this:
public class KPI : DependencyObject
{
public static readonly DependencyProperty DepartmentProperty = DependencyProperty.Register("Department", typeof(string), typeof(KPI), new UIPropertyMetadata(null));
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value); }
}
public static readonly DependencyProperty KPINumberProperty = DependencyProperty.Register("KPINumberProperty", typeof(int), typeof(KPI), new UIPropertyMetadata(null));
public int KPINumber
{
get { return (int)GetValue(KPINumberProperty); }
set { SetValue(KPINumberProperty, value); }
}
}
My idea was to have something like:
public static bool DataModified = false;
public static readonly DependencyProperty DepartmentProperty = DependencyProperty.Register("Department", typeof(string), typeof(KPI), new UIPropertyMetadata(null));
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value); DataModified = true; }
}
So every time something is edited the DataModified property will be set to TRUE, is this a good way of doing it? Or has somebody got a much better way of doing it?
Thanks in advance.
SumGuy.
This actually won't work if you're binding to the dependency property. The WPF binding engine does not actually use your CLR "Department" property, but rather uses "SetValue" on the dependency property directly. There is an easy solution to this though.
The UIPropertyMetadata has a field for a PropertyChangedCallback which will fire every time the value of the property is changed (either from a call to SetValue directly, or through the CLR property which is wrapping the SetValue call)
Here's an example:
public static readonly DependencyProperty DepartmentProperty =
DependencyProperty.Register("Department",
typeof(string),
typeof(KPI),
new UIPropertyMetadata(null, DepartmentPropertyChanged));
private static void DepartmentPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
KPI me = d as KPI;
if (me == null) return;
// Talk to your Business/Data layers here
}
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value); }
}
The DependencyObject (d) is the object who the property belongs to. In your case, this would be an instance of KPI.
For reference, here's a link to the UIPropertyMetadata MSDN documentation: http://msdn.microsoft.com/en-us/library/system.windows.uipropertymetadata.aspx
WPF binding system doesn't necessarily call the Department CLR property, instead it directly calls SetValue whenever it updates dependency property (in this case Department). That means, your CLR wrapper may not get called, which in turns means whatever code you've written in set block (of Department CLR property) would not be executed.
But don't worry, there is solution for it. While initializing DepartmentProperty you can pass a callback to UIPropertyMetadata which would be called everytime it updates the dependency property. All it means is that, you've to implement this:
public static readonly DependencyProperty DepartmentProperty = DependencyProperty.Register
(
"Department",
typeof(string),
typeof(KPI),
new UIPropertyMetadata(null, OnDepartmentChanged)
);
public string Department
{
get { return (string)GetValue(DepartmentProperty); }
set { SetValue(DepartmentProperty, value);}
}
static void OnDepartmentChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
(d as KPI).DataModified = true; //this is all you want!
}

Categories