I'm fairly new to WPF.
Suppose I defined an int dependency property. The purpose of the DP is to return the value+1 (see code). In .Net 2.0 I would have written:
private int _myValue = 0;
public int MyValue
{
get { return _myValue + 1; }
set { _myValue = value; }
}
How would you declare a DP that achieves similar behavior?
The Coercion that was offered works only for the Set operation. I would like to modify the Get result.
You would achieve it indirectly like this:
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(OwnerClass),
new FrameworkPropertyMetadata(0, null, new CoerceValueCallback(CoerceValue)));
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
private static object CoerceValue(DependencyObject d, object value)
{
return (int) value + 1;
}
Check this link for an explanation about coercion.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.