I have two Dependency Property, Value and MinVal.
I want the default value of "Value" will depend on "MinVal".
The "MinVal" set by xaml only one time.
How can I do that?
Here is the code:
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
// Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int), typeof(NumericHexUpDown), new UIPropertyMetadata(0, ValueChanged));
private static void ValueChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}
public int MinVal
{
get { return (int)GetValue(MinValProperty); }
set { SetValue(MinValProperty, value); }
}
// Using a DependencyProperty as the backing store for MinVal. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MinValProperty =
DependencyProperty.Register("MinVal", typeof(int), typeof(NumericHexUpDown), new UIPropertyMetadata(0, MinValueChanged));
private static void MinValueChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}
Basically you add coerce methods to your dependency properties. Your default values are in the metadata. But you want the values to react to each other before they are displayed after the XAML is loaded, and coercion does just that.
private static void OnMinValChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
((NumericHexUpDown)d).CoerceValue(ValueProperty);
}
private static void OnValueChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
((NumericHexUpDown)d).CoerceValue(MinValProperty);
}
private static object CoerceMinVal(DependencyObject d,
object value)
{
double min = ((NumericHexUpDown)d).MinVal;
return value;
}
private static object CoerceValue(DependencyObject d,
object value)
{
double min = ((NumericHexUpDown)d).MinVal;
double val = (double)value;
if (val < min) return min;
return value;
}
The metadata constructor looks like this
public static readonly DependencyProperty MinValProperty = DependencyProperty.Register(
"MinVal", typeof(int), typeof(NumericHexUpDown),
new FrameworkPropertyMetadata(
0,
new PropertyChangedCallback(OnMinimumChanged),
new CoerceValueCallback(CoerceMinimum)
),
);
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(int), typeof(NumericHexUpDown),
new FrameworkPropertyMetadata(
0,
new PropertyChangedCallback(OnValueChanged),
new CoerceValueCallback(CoerceValue)
),
);
References
http://drwpf.com/blog/2010/05/05/value-coercion-for-the-masses/
http://msdn.microsoft.com/en-us/library/ms745795.aspx
Related
I have three dependency property. TestControlHeight, HalfValue1 and HalfValue2. Now, depending on these three value I have to calculate
a third value which will be assigned as the height of the inner control which will also be a dependency property.
height of inner control = TestControlHeight/ (HalfValue1 - HalfValue2);
Where can I write this code to calculate the height of the inner control (which si also a dependency property)
public static readonly DependencyProperty TestControlHeightProperty =
DependencyProperty.Register("TestControlHeight", typeof (double),
typeof (TestControl), new PropertyMetadata(default(double)));
public double TestControlHeight
{
get { return (double) GetValue(TestControlHeightProperty); }
set { SetValue(TestControlHeightProperty, value); }
}
public static readonly DependencyProperty HalfValue1Property =
DependencyProperty.Register("HalfValue1", typeof (double), typeof
(TestControl), new PropertyMetadata(default(double)));
public double HalfValue1
{
get { return (double) GetValue(HalfValue1Property); }
set { SetValue(HalfValue1Property, value); }
}
public static readonly DependencyProperty HalfValue2Property =
DependencyProperty.Register("HalfValue2", typeof (double), typeof
(TestControl), new PropertyMetadata(default(double)));
public double HalfValue2
{
get { return (double) GetValue(HalfValue2Property); }
set { SetValue(HalfValue2Property, value); }
}
Thanks & Regards,
From the WPF Unleashed book:
.NET property wrappers are bypassed at runtime when setting dependency properties
in XAML!
Although the XAML compiler depends on the property wrapper at compile time, WPF calls the underlying GetValue and SetValue methods directly at runtime! Therefore, to maintain parity between setting a property in XAML and procedural code, it’s crucial that property wrappers not contain any logic in addition to the GetValue/SetValue calls.
If you want to add custom logic, that’s what the registered callbacks are for. All of WPF’s built-in property wrappers abide by this rule, so this warning is for anyone writing a custom class with its own dependency properties.
So, your code could look like this (not tested):
public static readonly DependencyProperty TestControlHeightProperty =
DependencyProperty.Register("TestControlHeight", typeof(double), typeof(TestControl),
new PropertyMetadata(false, new PropertyChangedCallback(OnHeightChanged)));
public double TestControlHeight
{
get { return (double)GetValue(TestControlHeightProperty); }
set { SetValue(TestControlHeightProperty, value); }
}
public static readonly DependencyProperty HalfValue1Property =
DependencyProperty.Register("HalfValue1", typeof(double), typeof(TestControl),
new PropertyMetadata(false, new PropertyChangedCallback(OnHeightChanged)));
public double HalfValue1
{
get { return (double)GetValue(HalfValue1Property); }
set { SetValue(HalfValue1Property, value); }
}
public static readonly DependencyProperty HalfValue2Property =
DependencyProperty.Register("HalfValue2", typeof(double), typeof(TestControl),
new PropertyMetadata(false, new PropertyChangedCallback(OnHeightChanged)));
public double HalfValue2
{
get { return (double)GetValue(HalfValue2Property); }
set { SetValue(HalfValue2Property, value); }
}
public double MyInnerControlHeight
{
get { return (double)GetValue(MyInnerControlHeightPropertyProperty); }
set { SetValue(MyInnerControlHeightPropertyProperty, value); }
}
public static readonly DependencyProperty MyInnerControlHeightPropertyProperty =
DependencyProperty.Register("MyInnerControlHeightProperty", typeof(double), typeof(TestControl),
new PropertyMetadata(0));
private static void OnHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var testControl = d as TestControl;
if (testControl != null)
{
testControl.MyInnerControlHeight = testControl.TestControlHeight / (testControl.HalfValue1 - testControl.HalfValue2);
}
}
How to disable calling callback methods in MyDP1_Changed and MyDP1_Changed?
I need dissable callback when I change value from other callback method
class MyDependencyObject : DependencyObject
{
public object MyDP1 {
get{ return GetValue(MyDP1Property); }
set{ return SetValue(MyDP1Property, value); }
}
public object MyDP2 {
get{ return GetValue(MyDP2Property); }
set{ return SetValue(MyDP2Property, value); }
}
public static readonly DependencyProperty MyDP1Property =
DependencyProperty.Register("MyDP1", typeof(object), typeof(MyDependencyObject )
, new UIPropertyMetadata(MyDP1_Changed));
public static readonly DependencyProperty MyDP2Property =
DependencyProperty.Register("MyDP2", typeof(object), typeof(MyDependencyObject )
, new UIPropertyMetadata(MyDP2_Changed));
private static void MyDP1_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
d.SetValue(MyDP2Property, e.NewValue);
}
private static void MyDP2_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
d.SetValue(MyDP1Property, e.OldValue);
}
}
OK firstly, you need to use SetCurrentValue not SetValue - otherwise you'll blow your bindings.
Secondly, you can use these methods to temporarily add and remove callbacks
TypeDescriptor.GetProperties(d)["MyDP1"].RemoveValueChanged(d, MyDP1_Changed)
TypeDescriptor.GetProperties(d)["MyDP1"].AddValueChanged(d, MyDP1_Changed)
I want to make a dependency property whose value depend on other dependency property but still settable/writable. For example, ‘DateOfBirth’ is an independent DependencyProperty. Now I want to make another DependencyProperty with name YearOfBirth. When DateOfBirth with Change it will Coerce the value of YearOfBirth property. i.e. d.CoerceValue(YearOfBirthProperty).
But How can I make dependent dependency property (e.g. YearOfBirth) writeable/settable?
If I code using CLR Properties, it works fine but how can do by using Dependency Properties!
What about this:
public partial class MainWindow : Window
{
public static DependencyProperty DateofBirthProperty = DependencyProperty.Register("DateofBirth", typeof(DateTime), typeof(MainWindow), new FrameworkPropertyMetadata(new PropertyChangedCallback(DateofBirth_Changed)));
public DateTime DateofBirth
{
get { return (DateTime)GetValue(DateofBirthProperty); }
set { SetValue(DateofBirthProperty, value); }
}
private static void DateofBirth_Changed(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
MainWindow thisClass = (MainWindow)o;
thisClass.SetDateofBirth();
}
private void SetDateofBirth()
{
DOBYear = DateofBirth.Year;
}
public static DependencyProperty DOBYearProperty = DependencyProperty.Register("DOBYear", typeof(int), typeof(MainWindow), new FrameworkPropertyMetadata(new PropertyChangedCallback(DOBYear_Changed)));
public int DOBYear
{
get { return (int)GetValue(DOBYearProperty); }
set { SetValue(DOBYearProperty, value); }
}
private static void DOBYear_Changed(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
MainWindow thisClass = (MainWindow)o;
thisClass.SetDOBYear();
}
private void SetDOBYear()
{
//Put Instance DOBYear Property Changed code here
}
public MainWindow()
{
InitializeComponent();
}
}
I Create a TimeInput Control Like to Enter Time.
<TextBox Text="{Binding Path=Hours}" />
<TextBox IsReadOnly="True"
Focusable="False"
Text=":" />
<TextBox Text="{Binding Path=Minutes}" />
and
public int Hours {
get { return (int)this.GetValue(HoursProperty); }
set {
this.SetValue(HoursProperty, value);
this.OnPropertyChanged("Hours");
}
}
public static readonly DependencyProperty HoursProperty =
DependencyProperty.Register("Hours", typeof(int), typeof(UserControl1), new UIPropertyMetadata(0, new PropertyChangedCallback(OnHoursChanged)));
private static void OnHoursChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
if (obj != null) {
int newValue = (int)e.NewValue;
}
}
public int Minutes {
get { return (int)this.GetValue(MinutesProperty); }
set {
this.SetValue(MinutesProperty, value);
this.OnPropertyChanged("Minutes");
}
}
// Using a DependencyProperty as the backing store for Minutes. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MinutesProperty =
DependencyProperty.Register("Minutes", typeof(int), typeof(UserControl1), new UIPropertyMetadata(0, new PropertyChangedCallback(OnMinutesChanged)));
private static void OnMinutesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
if (obj != null) {
int newValue = (int)e.NewValue;
}
}
public Nullable<TimeSpan> Value {
get { return (Nullable<TimeSpan>)this.GetValue(ValueProperty); }
set {
this.SetValue(ValueProperty, value);
this.OnPropertyChanged("Value");
}
}
// Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(Nullable<TimeSpan>), typeof(UserControl1), new UIPropertyMetadata(null, new PropertyChangedCallback(OnValueChanged)));
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
if (obj != null) {
(obj as UserControl1).UpdateTime(e.NewValue as TimeSpan?);
}
}
public void UpdateTime(TimeSpan? newTimeSpan) {
if (newTimeSpan.HasValue) {
this.Hours = newTimeSpan.Value.Hours;
this.Minutes = newTimeSpan.Value.Minutes;
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name) {
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
While I use this on another UserControl and Bind to a Property It doesn't work and show values.
I use it like this:
<uc:UserControl1 Value="{Binding StartTime}"/>
and
public TimeSpan StartTime
{
get { return new Types.Time(Item.StartTime).ToTimeSpan(); }
set { Item.StartTime = new Types.Time(value).ToShort(); NotifyPropertyChanged("StartTime"); }
}
That Item is an entity read from database and bind and StartTime is short form of hhmm.
i have updated your code, with dependency properties you don't need fire the property changed event explicit.
public partial class UserControl1 : UserControl
{
public UserControl1() {
this.InitializeComponent();
}
public int Hours {
get { return (int)this.GetValue(HoursProperty); }
set { this.SetValue(HoursProperty, value); }
}
public static readonly DependencyProperty HoursProperty =
DependencyProperty.Register("Hours", typeof(int), typeof(UserControl1), new UIPropertyMetadata(0, new PropertyChangedCallback(OnHoursChanged)));
private static void OnHoursChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
var uc = obj as UserControl1;
if (uc != null && e.NewValue != e.OldValue) {
int newValue = (int)e.NewValue;
uc.TimeValue = new TimeSpan(newValue, uc.Minutes, 0);
}
}
public int Minutes {
get { return (int)this.GetValue(MinutesProperty); }
set { this.SetValue(MinutesProperty, value); }
}
// Using a DependencyProperty as the backing store for Minutes. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MinutesProperty =
DependencyProperty.Register("Minutes", typeof(int), typeof(UserControl1), new UIPropertyMetadata(0, new PropertyChangedCallback(OnMinutesChanged)));
private static void OnMinutesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
var uc = obj as UserControl1;
if (uc != null && e.NewValue != e.OldValue) {
int newValue = (int)e.NewValue;
uc.TimeValue = new TimeSpan(uc.Hours, newValue, 0);
}
}
public Nullable<TimeSpan> TimeValue {
get { return (Nullable<TimeSpan>)this.GetValue(ValueProperty); }
set { this.SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("TimeValue", typeof(Nullable<TimeSpan>), typeof(UserControl1), new UIPropertyMetadata(null, new PropertyChangedCallback(OnValueChanged)));
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) {
var uc = obj as UserControl1;
if (uc != null && e.NewValue != e.OldValue) {
uc.UpdateTime(e.NewValue as TimeSpan?);
}
}
public void UpdateTime(TimeSpan? newTimeSpan) {
if (newTimeSpan.HasValue) {
this.Hours = newTimeSpan.Value.Hours;
this.Minutes = newTimeSpan.Value.Minutes;
}
}
}
second, i think you use the StartTime property incorrect, use it as dependency property too, or implement INotifyPropertyChanged.
{
// .....
StartTime = new Types.Time(this.Item.StartTime).ToTimeSpan();
// .....
}
public static readonly DependencyProperty StartTimeProperty =
DependencyProperty.Register("StartTime", typeof(TimeSpan?), typeof(Window1), new PropertyMetadata(default(TimeSpan?), new PropertyChangedCallback(OnStartTimePropertyChanged)));
private static void OnStartTimePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) {
if(e.NewValue != e.OldValue) {
(dependencyObject as Window1).Item.StartTime = new Types.Time(e.NewValue).ToShort();
}
}
public TimeSpan? StartTime {
get { return (TimeSpan?)GetValue(StartTimeProperty); }
set { SetValue(StartTimeProperty, value); }
}
hope this helps
You should not have any other code then calling GetValue and SetValue inside getter and setter of dependency property. But this may not resolve you problem. If you want to call some code when value change then do that inside callback method instead of setter.
I have a custom control that has a Dependancy Property...it has a few but let us say Dragable is my problem. The property is a boolean and I want to execute a piece of code each time it changes...a toggle.
I have two options, both shown below
[Category("Modal Options")]
public bool Dragable
{
get { return (bool)GetValue(DragableProperty); }
set { SetValue(DragableProperty, value); toggleDragable(); }
}
// Using a DependencyProperty as the backing store for Dragable. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DragableProperty =
DependencyProperty.Register("Dragable", typeof(bool),
typeof(PlussWindow), new PropertyMetadata(false));
private void MakeDragable()
{
this.dragBehavior.Attach(this.LayoutRoot);
}
private void MakeUnDragable()
{
this.dragBehavior.Detach();
}
public virtual void toggleDragable()
{
if (this.Dragable)
{
MakeUnDragable();
}
else
{
MakeDragable();
}
}
or
[Category("Modal Options")]
public bool Dragable
{
get { return (bool)GetValue(DragableProperty); }
set { SetValue(DragableProperty, value); }
}
// Using a DependencyProperty as the backing store for Dragable. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DragableProperty =
DependencyProperty.Register("Dragable", typeof(bool),
typeof(PlussWindow), new PropertyMetadata(false, (o, e) => { (o as PlussWindow).toggleDragable(); }
));
private void MakeDragable()
{
this.dragBehavior.Attach(this.LayoutRoot);
}
private void MakeUnDragable()
{
this.dragBehavior.Detach();
}
public virtual void toggleDragable()
{
if (this.Dragable)
{
MakeUnDragable();
}
else
{
MakeDragable();
}
}
Each method results in a 'Object reference not set to an instance of an object'
I usually use binding to get over this problem, e.g Visibility or Text are easily done, but for custom functionality I need to enable this in code.
How do I do this, noting that the propertychanged method is static?
Try this:
public bool Dragable
{
get { return (bool)GetValue(DragableProperty); }
set { SetValue(DragableProperty, value); }
}
public static readonly DependencyProperty DragableProperty =
DependencyProperty.Register("Dragable", typeof(bool), typeof(PlussWindow), new PropertyMetadata(false, new PropertyChangedCallback(onDragableChange)));
private static void onDragableChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
bool validate = (bool)e.NewValue;
PlussWindow win = (PlussWindow) d;
if (validate)
{
win.dragBehavior.Attach(this.LayoutRoot);
}
else
{
win.dragBehavior.Detach();
}
}