How can I update the source of a bound custom dependency property?

This should be possible via BindingExpressions try something like this.

Up vote 1 down vote favorite share g+ share fb share tw.

I have a custom DepenencyProperty which determines a UserControl's visibility. It is usually bound to a boolean value, however I would like to set it False when the Escape key is hit. The problem is, I don't want to overwrite the binding, I want to update the bindings source value.

How can I do this in code behind? For example, with this XAML I want to update the value of IsControlVisible to false, not MyControl. IsVisible wpf xaml binding dependency-properties link|improve this question asked Apr 7 '11 at 16:09Rachel21.9k11462 95% accept rate.

This should be possible via BindingExpressions, try something like this: private void MyControl_KeyDown(object sender, KeyEventArgs e) { if (e. Key == Key. Escape) { var source = sender as FrameworkElement; var expression = source.

GetBindingExpression(UIElement. IsVisibleProperty); (expression. DataItem as MyDataItem).

IsControlVisible = false; } } (If you do not reuse the UIElement. IsVisibleProperty you need to specify it via MyControl. IsVisibleProperty of course) Here is a reflection-using method: var source = sender as FrameworkElement; var expression = source.

GetBindingExpression(UIElement. IsVisibleProperty); var dataType = expression.DataItem.GetType(); dataType.GetProperties(). Single(x => x.

Name == expression.ParentBinding.Path. Path) . SetValue(expression.

DataItem, false, null).

The MyDataItem class is not a static class, so I don't think this will work. Multiple ViewModels contain this type of control and each use different boolean value to determine visibility. – Rachel Apr 7 '11 at 16:15 MyDataItem is not supposed to be a static class, this is just a cast to your object which holds the property IsControlVisible.

– H.B. Apr 7 '11 at 16:18 @HB I can't do that though because I don't know what class the Visibility is bound to, or even what the boolean property is called. ViewModelA might contain this UserControl and bind to the property ViewModelA. IsControlVisible while ViewModelB might bind to ViewModelB.

CanEdit – Rachel Apr 7 '11 at 16:24 Yuck... Anyway, I added a method which hopefully works in your case. – H.B. Apr 7 '11 at 16:34 @HB Thank you, that works perfectly :) – Rachel Apr 7 '11 at 16:38.

Use SetCurrentValue: this. SetCurrentValue(IsControlVisibleProperty, false); This won't overwrite the binding, but will instead push false to the binding source.

That doesn't work. It looks like it replaces the Binding on the property with false, so I lose the binding – Rachel Apr 7 '11 at 17:43 Are you sure you used SetCurrentValue and not SetValue? – H.B. Apr 7 '11 at 19:14 Just noticed that the mode of the Binding should be set to TwoWay when using this method.

Otherwise the source will not be changed. – H.B. Apr 7 '11 at 19:30 Also: +1, this is easily the best way of doing this. – H.B. Apr 7 '11 at 19:36 @HB Yes I was using SetCurrentValue.

The BindingMode was left to default and I'm not sure if it defaults to OneWay or TwoWay – Rachel Apr 7 '117 at 12:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions