<Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBlock}">
<SetterProperty="local:CommandBehavior.Event" Value="MouseLeftButtonDown"/>
<SetterProperty="local:CommandBehavior.Command"
Value="{BindingDataContext.TextBlockMouseLeftButtonDownCommand,RelativeSource={RelativeSourceSelf}}"/>
</Style>
像这段代码里的local:CommandBehavior是怎么弄的。
public class CommandBinder
{
public static object GetParameter(DependencyObject obj)
{
return (object)obj.GetValue(ParameterProperty);
}
public static void SetParameter(DependencyObject obj, object value)
{
obj.SetValue(ParameterProperty, value);
}
public static readonly DependencyProperty ParameterProperty =
DependencyProperty.RegisterAttached("Parameter", typeof(object), typeof(CommandBinder), new UIPropertyMetadata(null));
public static string GetEventName(DependencyObject obj)
{
return (string)obj.GetValue(EventNameProperty);
}
public static void SetEventName(DependencyObject obj, string value)
{
obj.SetValue(EventNameProperty, value);
}
public static readonly DependencyProperty EventNameProperty =
DependencyProperty.RegisterAttached("EventName", typeof(string), typeof(CommandBinder), new UIPropertyMetadata(null));
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(CommandBinder), new PropertyMetadata(ChangedCallback));
private static void ChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = d as FrameworkElement;
if (element != null)
{
string eventName = element.GetValue(EventNameProperty) as string;
if (eventName != null)
{
EventInfo eventInfo = element.GetType().GetEvent(eventName);
var handler = new MouseButtonEventHandler((sender, arg) =>
{
object obj = element.GetValue(ParameterProperty);
(e.NewValue as ICommand).Execute(obj);
});
var del = handler.GetInvocationList()[0];
eventInfo.AddEventHandler(element, del);
}
}
}
}
类似这种类 怎么让MouseButtonEventHandler这个可以不固定
local
表示啥?
在根元素里定义的 代表某个命名空间
先这样吧~