I’m brand new to WPF, so I’m a bit at a loss for how to do this. I created a very simple visual UserControl today that consists of a grid, displaying a circle with two dotted lines crossing through it. The intent is to display a circle of a given radius size. I’d like to be able to hide Height and Width, and use a custom property called RadiusSize to set the size of the control.
The Code Behind defines the RadiusSize dependency property, as such:
public static readonly DependencyProperty RadiusSizeProperty =
DependencyProperty.Register("RadiusSize", typeof(double), typeof(Radius));
public double RadiusSize
{
get { return (double)GetValue(RadiusSizeProperty); }
set
{
SetValue(RadiusSizeProperty, value);
Height = value * 2;
Width = value * 2;
RaisePropertyChanged("RadiusSize");
}
}
There’s no visible change to the Height or Width property when setting up the XAML for a given Radius control and passing it a RadiusSize unfortunately. If removed, they just default to Auto. Is what I want to do possible?
Yes, it is possible. No, you are not doing it right.
Its a bit hard to tell how you’re setting width/height on the circle, but the obvious newbie error you have in your code is that you’re doing stuff within the setter of a DependencyProperty.
DP
Bindings are not accessed through the getters/setters of your property. The binding system works behind the scenes, so if you bind to a DP on your class the getters/setters won’t be called when the bindings are updated.So, what are the alternatives? Many. You can add a PropertyMetadata instance to the property declaration that specifies a callback, and in that callback update other properties… or you can avoid it altogether. I’d go with the second.
If I were going to create a UserControl with an ellipse and some other drawings in it, I’d just slap the visuals in a ViewBox and have it automatically stretch to the size of the UserControl, and forget the whole radius thing.
But if you’re still interested in the radius, you could use an IValueConverter to do the work.
Then, define a
Radiusproperty in your UserControl as you have, and