WPF/Silverlight 中的标记扩展

发布于 2024-07-14 07:29:49 字数 80 浏览 13 评论 0原文

有人曾经在 WPF 或 Silverlight 中创建过自定义标记扩展吗? 您什么时候想要或需要这样做? 有关如何做到这一点的任何提示或来源?

Has anyone ever created a custom markup extension in WPF or Silverlight? When would you ever want or need to do this? Any tips or sources on how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

童话里做英雄 2024-07-21 07:29:49

例如本地化

本地化应用程序资源的一种简单而有效的方法是
编写一个提供本地化值的自定义 MarkupExtension。 这
扩展采用唯一的参数
资源键...[然后]从通用资源提供程序查找值。

注意:您无法在 silverlight 中编写自定义标记扩展。

An example would be for Localization:

A simple and effective way to localize application resources is to
write a custom MarkupExtension that provides a localized value. The
extension takes a parameter that is a unique
resource key... [and then] looks up the value from a generic resource provider.

Note: You can not write custom markup extensions in silverlight.

随风而去 2024-07-21 07:29:49

是的,它很方便,我自己也创建了一个。 我创建了一个名为 EvalBinding 的标记扩展,它采用一组绑定作为子项和一个 C# 评估字符串。 它评估 C# 来处理子绑定中的值,因此我不需要创建许多简单的 TypeConverter 类。

例如,我可以这样做...

<EvalBinding Eval="(this[0] > this[1] ? 'GT' : 'LTE')">
    <Binding ElementName="element1" Path="Size"/>
    <Binding ElementName="element2" Path="Size"/>
<EvalBinding>

其中 this 是对子绑定结果数组的引用。

有关实现 MarkupExtension 的资源...

MSDN

乔什·史密斯博客条目

< a href="http://www.windows-now.com/blogs/rrelyea/archive/2006/02/03/MarkupExtensionAsAnObjectElement.aspx" rel="nofollow noreferrer">Rob Relyea 博客条目

Yes it is handy and I have created one myself. I created a markup extension called EvalBinding that takes a set of bindings as children and a C# evaluation string. It evaluates the C# to process the values from the child bindings so that I do not need to create many simple TypeConverter classes.

For example I can do this...

<EvalBinding Eval="(this[0] > this[1] ? 'GT' : 'LTE')">
    <Binding ElementName="element1" Path="Size"/>
    <Binding ElementName="element2" Path="Size"/>
<EvalBinding>

Where this is a reference to the array of child binding results.

For resources on implementing a MarkupExtension...

MSDN

Josh Smith Blog Entry

Rob Relyea Blog Entry

关于从前 2024-07-21 07:29:49

万岁!!

这是在 Silverlight 5 中实现的!

而且,现在它是一个通用接口而不是一个类!

查看一下< /a>.

阅读 this 为例。

Hooray!!

This is implemented in Silverlight 5!!

And furthermore, now it's a generic interface instead of a class!!

Check it out.

Read this for an example.

坚持沉默 2024-07-21 07:29:49

我使用标记扩展来标准化我的验证绑定。 所以这里的好处很小,其中 4 个默认值我不必再设置了,如果我将来想更改它们,我只需在这里进行即可。

using System;
using System.Windows.Data;
using System.Windows.Markup;

namespace ITIS 
{
    /// <summary>
    /// Creates a normal Binding but defaults NotifyOnValidationError to True,
    /// ValidatesOnExceptions to True, Mode to TwoWay and 
    /// UpdateSourceTrigger to LostFocus.
    /// </summary>
    public sealed class ValidatedBinding : MarkupExtension
    {
        public ValidatedBinding(string path)
        {
            Mode = BindingMode.TwoWay;

            UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;

            Path = path;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return new Binding(Path) {
                Converter = this.Converter,
                ConverterParameter = this.ConverterParameter,
                ElementName = this.ElementName,
                FallbackValue = this.FallbackValue,
                Mode = this.Mode,
                NotifyOnValidationError = true,
                StringFormat = this.StringFormat,
                ValidatesOnExceptions = true,
                UpdateSourceTrigger = this.UpdateSourceTrigger
            };
        }

        public IValueConverter Converter { get; set; }

        public object ConverterParameter { get; set; }

        public string ElementName { get; set; }

        public object FallbackValue { get; set; }

        public BindingMode Mode { get; set; }

        public string Path { get; set; }

        public string StringFormat { get; set; }

        public UpdateSourceTrigger UpdateSourceTrigger { get; set; }
    }
}

I use a markup extension to standardise my validation bindings. So the benefit here is small, 4 of the defaults I don't have to set anymore, and if I wish to change them in the future, I do it here only.

using System;
using System.Windows.Data;
using System.Windows.Markup;

namespace ITIS 
{
    /// <summary>
    /// Creates a normal Binding but defaults NotifyOnValidationError to True,
    /// ValidatesOnExceptions to True, Mode to TwoWay and 
    /// UpdateSourceTrigger to LostFocus.
    /// </summary>
    public sealed class ValidatedBinding : MarkupExtension
    {
        public ValidatedBinding(string path)
        {
            Mode = BindingMode.TwoWay;

            UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;

            Path = path;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return new Binding(Path) {
                Converter = this.Converter,
                ConverterParameter = this.ConverterParameter,
                ElementName = this.ElementName,
                FallbackValue = this.FallbackValue,
                Mode = this.Mode,
                NotifyOnValidationError = true,
                StringFormat = this.StringFormat,
                ValidatesOnExceptions = true,
                UpdateSourceTrigger = this.UpdateSourceTrigger
            };
        }

        public IValueConverter Converter { get; set; }

        public object ConverterParameter { get; set; }

        public string ElementName { get; set; }

        public object FallbackValue { get; set; }

        public BindingMode Mode { get; set; }

        public string Path { get; set; }

        public string StringFormat { get; set; }

        public UpdateSourceTrigger UpdateSourceTrigger { get; set; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文