首页 新闻 会员 周边

关于 wpf 中EventSetter CommandBehaviors的问题

0
悬赏园豆:20 [已关闭问题] 关闭于 2013-08-09 14:18
<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这个可以不固定

星星火燎的主页 星星火燎 | 初学一级 | 园豆:185
提问于:2013-08-09 11:10
< >
分享
所有回答(3)
0

local  表示啥?

Launcher | 园豆:45045 (高人七级) | 2013-08-09 11:14
0

在根元素里定义的 代表某个命名空间

Yu | 园豆:12980 (专家六级) | 2013-08-09 11:19
0

先这样吧~

星星火燎 | 园豆:185 (初学一级) | 2013-08-09 14:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册