GetValue and SetValue is not called on binding - c#

I wrote a custom control derived from Progressbar which implements animations on valuechange (the value fills up with a doubleanimation until the target is reached).
var duration = new Duration(TimeSpan.FromSeconds(2.0));
var doubleanimation = new DoubleAnimation(value, duration)
{
EasingFunction = new BounceEase()
};
BeginAnimation(ValueProperty, doubleanimation);
A new Property "TargetValue" is used, because the ControlTemplate used for the ProgressBar has to show the new value straight after changing it. For this the ProgressEx contains the following:
public static readonly DependencyProperty TargetValueProperty = DependencyProperty.Register("TargetValue", typeof (int), typeof (ProgressEx), new FrameworkPropertyMetadata(0));
public int TargetValue
{
get { return (int)GetValue(TargetValueProperty); }
set
{
if (value > Maximum)
{
//Tinting background-color
_oldBackground = Background;
Background = FindResource("DarkBackgroundHpOver100") as LinearGradientBrush;
}
else
{
if (_oldBackground != null)
Background = _oldBackground;
}
SetValue(TargetValueProperty, value);
Value = value;
}
}
When the TargetValue exceeds the maximum i will use a different color defined in xaml. This works really good - But. Now i want to use this bar in a listview where it is bind to some data. Problem is, the setter is not called in this case, so no animation is executed, even when the value is changed via TargetValue={Binding ProgressValue}. I know that the framework will always call GetValue and SetValue directly and no logic should be supplied, but is there a way around this?
Thanks in advance.

The CLR style getters and setters of DependencyPropertys are not meant to be called by the Framework... they are there for developers to use in code. If you want to know when a DependencyProperty value has changed, you need to add a handler:
public static readonly DependencyProperty TargetValueProperty =
DependencyProperty.Register("TargetValue", typeof (int), typeof (ProgressEx),
new FrameworkPropertyMetadata(0, OnTargetValueChanged));
private static void OnTargetValueChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
// Do something with the e.NewValue and/or e.OldValue values here
}

Related

WPF – Change Property Value in Setter of Another Property

In the following example of my custom control, why I can't change a TransparentColor property in a setter of a SelectedColor property? A solution with callback method works fine, what's the difference between them in the case of change another property?
public class MyColorPicker : Control
{
static MyColorPicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyColorPicker), new FrameworkPropertyMetadata(typeof(MyColorPicker)));
}
public static readonly DependencyProperty SelectedColorProperty =
DependencyProperty.Register("SelectedColor", typeof(Color), typeof(MyColorPicker), new PropertyMetadata(Color.FromRgb(0, 201, 201), OnSelectedColorChange));
private static void OnSelectedColorChange(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var colorPicker = (MyColorPicker)sender;
var selectedColor = colorPicker.SelectedColor;
colorPicker.TransparentColor = Color.FromArgb(0, selectedColor.R, selectedColor.G, selectedColor.B);
}
public Color SelectedColor
{
get { return (Color)GetValue(SelectedColorProperty); }
set
{
// Why this not working? BorderStopColor = Color.FromArgb(0, value.R, value.G, value.B);
SetValue(SelectedColorProperty, value);
}
}
private static readonly DependencyPropertyKey TransparentColorPropertyKey =
DependencyProperty.RegisterReadOnly("TransparentColor", typeof(Color), typeof(MyColorPicker), new PropertyMetadata(Color.FromArgb(0, 0, 201, 201)));
public static readonly DependencyProperty TransparentColorProperty = TransparentColorPropertyKey.DependencyProperty;
public Color TransparentColor
{
get { return (Color)GetValue(TransparentColorProperty); }
protected set { SetValue(TransparentColorPropertyKey, value); }
}
}
The setter of a CLR wrapper for a dependency property should only call the SetValue method to set the actual value of the dependency property.
Any other logic should be implemented in the callback.
Also note that the CLR property wrappers are bypassed at runtime when setting dependency properties in XAML (but the callbacks are not):
Why are .NET property wrappers bypassed at runtime when setting dependency properties in XAML?

WPF - GetBindingExpression in PropertyChangedCallback of DependencyProperty

I need to be able to access the binding expression for the Text property of a TextBox from within a DependencyProperty on a TextBox. the value of my DependencyProperty is set in XAML. I'm calling GetBindingExpression in the PropertyChangedCallback method of my DependencyProperty, but I'm too early at this point because GetBindingExpression always returns null here, yet after the window fully loads it definitely returns a value (I tested using a button on screen to change the value of my DependencyProperty).
Clearly I have a load order issue here where my DependencyProperty's value is set before the Text property is bound to my view model. My question is, is there some event I can hook into to identify when the binding of the Text property is complete? Preferably without modifying the XAML of my TextBox as I have hundreds of them in the solution.
public class Foobar
{
public static readonly DependencyProperty TestProperty =
DependencyProperty.RegisterAttached(
"Test", typeof(bool), typeof(Foobar),
new UIPropertyMetadata(false, Foobar.TestChanged));
private static void TestChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
var textBox = (TextBox)o;
var expr = textBox.GetBindingExpression(TextBox.TextProperty);
//expr is always null here, but after the window loads it has a value
}
[AttachedPropertyBrowsableForType(typeof(TextBoxBase))]
public static bool GetTest(DependencyObject obj)
{
return (bool)obj.GetValue(Foobar.TestProperty);
}
[AttachedPropertyBrowsableForType(typeof(TextBoxBase))]
public static void SetTest(DependencyObject obj, bool value)
{
obj.SetValue(Foobar.TestProperty, value);
}
}
Try listen to LayoutUpdated event. I think its called layout updated event. Else google it. Its a crazy little event which will be fired everytime no matter what you do ex. when loading, drawing, even when you move your mouse!
Take a look at this pseudo code:
private static void TestChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
var textBox = (TextBox)o;
// start listening
textBox.LayoutUpdated += SomeMethod();
}
private static void SomeMethod(...)
{
// this will be called very very often
var expr = textBox.GetBindingExpression(TextBox.TextProperty);
if(expr != null)
{
// finally you got the value so stop listening
textBox.LayoutUpdated -= SomeMethod();

WPF - DependencyProperty ignoring setter with side-effects on change [duplicate]

This question already has an answer here:
WPF: XAML property declarations not being set via Setters?
(1 answer)
Closed 7 years ago.
I have a WPF user control that is a wrapper for two other controls, showing only one of them depending on the situation. It possesses an ItemsSource property which sets the ItemsSource for the two underlying controls. I want to make it so that this property can be bound to on a .xaml file.
I created a DependencyProperty, and I've changed my getter and my setter to use it. However, when I debug the code, I can see that the setter is never getting called. I can see that the dependency property is changing its value, but it's not setting the underlying controls' properties.
How can I make it so that the underlying controls have their properties set when the dependency property changes?
public partial class AccountSelector : UserControl
{
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
"ItemsSource", typeof(IEnumerable), typeof(AccountSelector));
public IEnumerable ItemsSource
{
get
{
return (IEnumerable)GetValue(ItemsSourceProperty);
}
set
{
if (UseComboBox)
AccCombo.ItemsSource = value;
else
AccComplete.ItemsSource = value;
SetValue(ItemsSourceProperty, value);
}
}
}
You have to pass a propertyChangedCallback to your UIPropertyMetadata, like this:
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
"ItemsSource", typeof(IEnumerable), typeof(AccountSelector), new UIPropertyMetadata((d, e) =>
{
if (e.NewValue == null) return;
var s = d as AccountSelector;
var list = e.NewValue as IEnumerable;
if (list == null || s == null) return;
if (s.UseComboBox)
s.AccCombo.ItemsSource = list;
else
s.AccComplete.ItemsSource = list;
}));
public IEnumerable ItemsSource
{
get
{
return (IEnumerable)GetValue(ItemsSourceProperty);
}
set
{
SetValue(ItemsSourceProperty, value);
}
}

How can I update the source from a custom control with dependency properties in silverlight?

I've created a custom control that extends the RichTextBox so that I can create a binding for the xaml property. It all works well as long as I just update the property from the viewmodel but when I try to edit in the richtextbox the property is not updated back.
I have the following code in the extended version of the richtextbox.
public static readonly DependencyProperty TextProperty = DependencyProperty.Register ("Text", typeof(string), typeof(BindableRichTextBox), new PropertyMetadata(OnTextPropertyChanged));
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var rtb = d as BindableRichTextBox;
if (rtb == null)
return;
string xaml = null;
if (e.NewValue != null)
{
xaml = e.NewValue as string;
if (xaml == null)
return;
}
rtb.Xaml = xaml ?? string.Empty;
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
In the view I've set the binding like such
<Controls:BindableRichTextBox Text="{Binding XamlText, Mode=TwoWay}"/>
In the viewmodel I've created the XamlText as a normal property with the NotifyPropertyChanged event being called on updates.
I want the bound XamlText to be updated when the user enters texts in the RichTextBox either on lostfocus or directly during edit, it doesn't really matter.
How can I change the code to make this happen?
You will need to listen to changes to the Xaml-property of the BindableRichTextBox and set the Text-property accordingly. There is an answer available here describing how that could be achieved. Using the approach described in that would the result in the following code (untested):
public BindableRichTextBox()
{
this.RegisterForNotification("Xaml", this, (d,e) => ((BindableRichTextBox)d).Text = e.NewValue);
}
public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
{
var binding = new Binding(propertyName) { Source = element };
var property = DependencyProperty.RegisterAttached(
"ListenAttached" + propertyName,
typeof(object),
typeof(UserControl),
new PropertyMetadata(callback));
element.SetBinding(property, binding);
}

Storyboard doesn't animate custom FrameworkElement property

I have class that is derived from FrameworkElement, and I want WPF to update its Location property by using DoubleAnimation. I register the property as DependendencyProperty:
public class TimeCursor : FrameworkElement
{
public static readonly DependencyProperty LocationProperty;
public double Location
{
get { return (double)GetValue(LocationProperty); }
set
{
SetValue(LocationProperty, value);
}
}
static TimeCursor()
{
LocationProperty = DependencyProperty.Register("Location", typeof(double), typeof(TimeCursor));
}
}
Following code sets up the storyboard.
TimeCursor timeCursor;
private void SetCursorAnimation()
{
timeCursor = new TimeCursor();
NameScope.SetNameScope(this, new NameScope());
RegisterName("TimeCursor", timeCursor);
storyboard.Children.Clear();
DoubleAnimation animation = new DoubleAnimation(LeftOffset, LeftOffset + (VerticalLineCount - 1) * HorizontalGap + VerticalLineThickness,
new Duration(TimeSpan.FromMilliseconds(musicDuration)), FillBehavior.HoldEnd);
Storyboard.SetTargetName(animation, "TimeCursor");
Storyboard.SetTargetProperty(animation, new PropertyPath(TimeCursor.LocationProperty));
storyboard.Children.Add(animation);
}
Then I call storyboard.Begin(this) from another method of the object which contains the above SetCursorAnimation() method and this object is derived from Canvas. However the Location property is never updated(set accessor of Location is never called) and no exception is thrown. What am I doing wrong?
When a dependency property is animated (or set in XAML, or set by a Style Setter, etc.), WPF does not call the CLR wrapper, but instead directly accesses the underlying DependencyObject and DependencyProperty objects. See the Implementing the "Wrapper" section in Checklist for Defining a Dependency Property and also Implications for Custom Dependency Properties.
In order to get notified about property changes, you have to register a PropertyChangedCallback:
public class TimeCursor : FrameworkElement
{
public static readonly DependencyProperty LocationProperty =
DependencyProperty.Register(
"Location", typeof(double), typeof(TimeCursor),
new FrameworkPropertyMetadata(LocationPropertyChanged)); // register callback here
public double Location
{
get { return (double)GetValue(LocationProperty); }
set { SetValue(LocationProperty, value); }
}
private static void LocationPropertyChanged(
DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var timeCursor = obj as TimeCursor;
// handle Location property changes here
...
}
}
And note also that animating a dependency property does not necessarily require a Storyboard. You could simply call the BeginAnimation method on your TimeCursor instance:
var animation = new DoubleAnimation(LeftOffset,
LeftOffset + (VerticalLineCount - 1) * HorizontalGap + VerticalLineThickness,
new Duration(TimeSpan.FromMilliseconds(musicDuration)),
FillBehavior.HoldEnd);
timeCursor.BeginAnimation(TimeCursor.LocationProperty, animation);

Categories