Silverlight 4默认按钮服务 [英] Silverlight 4 Default Button Service

查看:83
本文介绍了Silverlight 4默认按钮服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月来,我一直成功地使用

For a few months I have been successfully using David Justices Default Button example in my SL 3 app. This approach is based on an attached property.

升级到SL4后,该方法不再起作用,并且出现XAML异常:

After upgrading to SL4, the approach no longer works, and I get a XAML exception:

未知的解析器错误:扫描器2148474880

Unknown parser error: Scanner 2148474880

有人成功使用了SL4中的此(或任何其他)默认按钮附加行为吗?

Has anyone succesfully used this (or any other) default button attached behaviours in SL4?

是否有其他方法可以使用可用的新类在SL4中实现默认按钮行为?

Is there any other way to achieve default button behaviour in SL4 with the new classes that are available?

谢谢, 标记

推荐答案

我通过允许在其他属性中设置自定义键(默认为Enter)来扩展了David的方法:

I extended David's approach by allowing a custom key (defaulted to Enter) to be set in an additional property:

    public static DependencyProperty ButtonKeyProperty = DependencyProperty.RegisterAttached(
         "ButtonKey",
         typeof(Key),
         typeof(Defaults),
         new PropertyMetadata(Key.Enter, ButtonChanged));

    public static void SetButtonKey(DependencyObject dependencyObj, Key key)
    {
        dependencyObj.SetValue(ButtonKeyProperty, key);
    }

    public static Key GetButtonKey(DependencyObject dependencyObj)
    {
        return (Key)dependencyObj.GetValue(ButtonKeyProperty);
    }

我修改了原始属性,然后利用此属性:

I modified the original property to then leverage this property:

    Key key = GetButtonKey(dependencyObj);
    if (button.IsEnabled && keyEvent.Key == key)
        ...

因此,例如,现在,我可以根据需要使用Escape作为键(请注意,我更改了类和属性的名称):

So now, for example, I can use Escape as the key if I want (note I changed the named of the classes and properties):

    ... UI:Defaults.Button="{Binding ElementName=myButton}" UI:Defaults.ButtonKey="Escape" ...

这篇关于Silverlight 4默认按钮服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆