For the sample xaml, I would go with a ValueConverter for the image in the Source parameter, and it would be something like this:
Considering you have a Property that describe the state of your control as an enumerable:
public enum ControlState
{
Normal = 0,
Pressed,
Disabled
}
Then you define the ValueConverter:
public class ControlStateToImageConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
ControlState state = (ControlState)value;
if (state != null)
{
BitmapImage img = null;
switch (state)
{
case ControlState.Normal:
img = new BitmapImage(
new Uri("pack://application:,,,/Resources/started01.png"));
break;
default:
img = new BitmapImage(
new Uri("pack://application:,,,/Resources/RedCircle.png"));
break;
}
return img;
}
return null;
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Add the converter to the resources of your user control:
<UserControl.Resources>
<local:ControlStateToImageConverter x:Key="ImgConverter"/>
</UserControl.Resources>
then you just do a simple binding in your image as:
<Image Name="Normal" HorizontalAlignment="Left" Width="40" Height="40"
Source="{Binding ElementName=self, Path=IsPressed, Converter={StaticResource ImgConverter}}"
/>
just remember to add a x:Name to your UserControl in this sample
x:Name="self"
This approach gives you some advantages like you dont need to render 3 Image Controls (even if they are Hidden, they are rendered) you can track the changes to CLR properties with the INotifyPropertyChanged or use DependencyProperty which will automaticaly track the changes in the property for you. But if you are really into making a control that allow templating you should start by using a class that extends from a base control of your choice, implement the DependencyProperties that you need, add the handlers that you need and add a static constructor like this:
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(CustomControl1),
new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
Remember also to add the TemplateParts that you need to use. I would strongly recomend that you read:
http://xamlcoder.com/cs/blogs/joe/archive/2007/12/13/building-custom-template-able-wpf-controls.aspx[
^]
Creating a look-less custom control in WPF[
^]
Hope it helps you.
All best