public static class LableUserControl
{
public static readonly DependencyProperty CommandProperty =DependencyProperty.RegisterAttached("Command"
,typeof(ICommand),typeof(LableUserControl),new FrameworkPropertyMetadata(null, CommandPropertyChanged));
public static ICommand GetCommand(Label label)
{
return (ICommand)label.GetValue(CommandProperty);
}
public static void SetCommand(Label label, ICommand value)
{
label.SetValue(CommandProperty, value);
}
private static void CommandPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var label = obj as Label;
if (label == null)
return;
label.MouseLeftButtonDown -= Label_MouseLeftButtonDown;
if (e.NewValue != null)
label.MouseLeftButtonDown += Label_MouseLeftButtonDown;
}
private static void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var label = sender as Label;
if (label == null)
return;
var command = GetCommand(label);
if (command != null && command.CanExecute(label.Content))
command.Execute(label.Content);
}
}