I'm trying to get some WPF concepts down, so I've put together a simple example of what I'm trying to do. I would like to set a custom property of a user control, and have it be used by an element within the control.
I've been researching and experimenting, but I'm not fully understanding everything here. Any help would be appreciated.
The user control for this example is a simple square with a circle inside of it:
<UserControl x:Class="CircleInSquare"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="100" Height="100" >
<Grid Background="#000000">
<Ellipse Name="Circle"
Margin="10"
Fill="?????????"
>
</Ellipse>
</Grid>
</UserControl>
The VB Code Behind it:
Partial Public Class CircleInSquare
Private _CircleColor As Color
Public Property CircleColor() As Color
Get
Return _CircleColor
End Get
Set(ByVal value As Color)
_CircleColor = value
End Set
End Property
End Class
When I use this user control, how can I apply a CircleColor to the control, and have it be shown as the Ellipse's fill color? Even better... can I give it a default color that shows up in the VS2008 Designer?
So... if I place one of these into my window XAML like this:
<app:CircleInSquare CircleColor="Blue" />
I would like the circle to display as Blue (or any other color I choose for that instance)
Set the DataContext for the ellipse to an instance of your CircleInSquare class. And make sure you use implement INotifyProperychanged on this class to make it property change enabled. check this link if you need more info on Propertychange
Sorry to repost, but After re-reading you post, I think that you might be better off with templating. I've attached some samples in VB
Window.xaml
CircleInSquare.xaml.vb
CircleInSquare.xaml
You set up a dependency property like this:
And then in your xaml you do something like this
and you can set a default style outside of the control template like this:
You might also be able to do it with a normal property but dependency properties support binding which makes styling much easier later.
HTH
You need to create a DependencyProperty in your CircleInSquare class. Do some googling on that. The concept of using the property for your circle class like below is called AttachedProperties, and you will probably need to handle the CircleColorChanged event to do what you need.