using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
namespace WpfClickableImage
{
/// <summary>
/// Logique d'interaction pour ControlClickableImage.xaml
/// </summary>
public partial class ControlClickableImage : UserControl
{
public ControlClickableImage()
{
InitializeComponent();
}
public ImageSource Image
{
get { return (ImageSource)GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
// Using a DependencyProperty as the backing store for Image. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageProperty =
DependencyProperty.Register("Image", typeof(ImageSource), typeof(ControlClickableImage),
new UIPropertyMetadata(null));
public ImageSource ImageBack
{
get { return (ImageSource)GetValue(ImageBackProperty); }
set { SetValue(ImageBackProperty, value); }
}
// Using a DependencyProperty as the backing store for ImageBack. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageBackProperty =
DependencyProperty.Register("ImageBack", typeof(ImageSource), typeof(ControlClickableImage),
new UIPropertyMetadata(null));
public double ImageWidth
{
get { return (double)GetValue(ImageWidthProperty); }
set { SetValue(ImageWidthProperty, value); }
}
// Using a DependencyProperty as the backing store for ImageWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageWidthProperty =
DependencyProperty.Register("ImageWidth", typeof(double), typeof(ControlClickableImage),
new UIPropertyMetadata(16d));
public double ImageHeight
{
get { return (double)GetValue(ImageHeightProperty); }
set { SetValue(ImageHeightProperty, value); }
}
// Using a DependencyProperty as the backing store for ImageHeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageHeightProperty =
DependencyProperty.Register("ImageHeight", typeof(double), typeof(ControlClickableImage),
new UIPropertyMetadata(16d));
public string Text1
{
get { return (string)GetValue(Text1Property); }
set { SetValue(Text1Property, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty Text1Property =
DependencyProperty.Register("Text1", typeof(string), typeof(ControlClickableImage),
new UIPropertyMetadata(""));
public string Text2
{
get { return (string)GetValue(Text2Property); }
set { SetValue(Text2Property, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty Text2Property =
DependencyProperty.Register("Text2", typeof(string), typeof(ControlClickableImage),
new UIPropertyMetadata(""));
public bool? IsChecked
{
get { return (bool?)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
//Using a DependencyProperty as the backing store for IsChecked. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsCheckedProperty =
ToggleButton.IsCheckedProperty.AddOwner(typeof (ControlClickableImage));
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
//les 2 events sont declenches ici
if ((bool)this.IsChecked)
{
OnCheckedChanged(this, e);
}
else if (!(bool)this.IsChecked)
{
OnUnCheckChanged(this, e);
}
}
// event perso Checked
public static readonly RoutedEvent CheckedEvent = EventManager.RegisterRoutedEvent(
"Checked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ControlClickableImage));
// Provide CLR accessors for the event
public event RoutedEventHandler Checked
{
add { AddHandler(CheckedEvent, value); }
remove { RemoveHandler(CheckedEvent, value); }
}
// event perso UnChecked
public static readonly RoutedEvent UnCheckedEvent = EventManager.RegisterRoutedEvent(
"UnChecked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ControlClickableImage));
// Provide CLR accessors for the event
public event RoutedEventHandler UnChecked
{
add { AddHandler(UnCheckedEvent, value); }
remove { RemoveHandler(UnCheckedEvent, value); }
}
// methods charge de raiser les 2 events
private void OnCheckedChanged(ControlClickableImage controlClickableImage, RoutedEventArgs e)
{
RoutedEventArgs newEventArgs = new RoutedEventArgs( ControlClickableImage.CheckedEvent);
newEventArgs.Source = controlClickableImage;
RaiseEvent(newEventArgs);
}
private void OnUnCheckChanged(ControlClickableImage controlClickableImage, RoutedEventArgs e)
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(ControlClickableImage.UnCheckedEvent);
newEventArgs.Source = controlClickableImage;
RaiseEvent(newEventArgs);
}
}
}
Partager