首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MarkupExtension:将简单属性转换为DependencyProperty

MarkupExtension:将简单属性转换为DependencyProperty
EN

Stack Overflow用户
提问于 2012-03-02 00:28:40
回答 1查看 1.9K关注 0票数 3

我正在使用WPFLocalizationExtension (在CodePlex上提供)来本地化我的WPF应用程序中的字符串。这个简单的MarkupExtension在下面这样的简单场景中工作得很好:

代码语言:javascript
复制
<Button Content="{lex:LocText MyApp:Resources:buttonTitle}" />

但当我尝试一些更复杂的东西时,我就卡住了,比如:

代码语言:javascript
复制
<Window Title="{lex:LocText MyApp:Resources:windowTitle, FormatSegment1={Binding Version}}" />

(使用资源windowTitle = "MyApp v{0}")。

因为FormatSegment1是一个普通的INotifyPropertyChange属性,所以我不能给它绑定任何东西。如果FormatSegment1是一个DependencyProperty,这是可能的,所以我下载了源代码并尝试对其进行修补。

我修改过了

代码语言:javascript
复制
[MarkupExtensionReturnType(typeof(string))]
public class LocTextExtension : BaseLocalizeExtension<string>
{

    // ---- OLD property

    //public string FormatSegment1
    //{
    //    get { return this.formatSegments[0]; }
    //    set
    //    {
    //        this.formatSegments[0] = value;
    //        this.HandleNewValue();
    //    }
    //}

    // ---- NEW DependencyProperty

    /// <summary>
    /// The <see cref="FormatSegment1" /> dependency property's name.
    /// </summary>
    public const string FormatSegment1PropertyName = "FormatSegment1";

    /// <summary>
    /// Gets or sets the value of the <see cref="FormatSegment1" />
    /// property. This is a dependency property.
    /// </summary>
    public string FormatSegment1
    {
        get
        {
            return (string)GetValue(FormatSegment1Property);
        }
        set
        {
            SetValue(FormatSegment1Property, value);
        }
    }

    /// <summary>
    /// Identifies the <see cref="FormatSegment1" /> dependency property.
    /// </summary>
    public static readonly DependencyProperty FormatSegment1Property = DependencyProperty.Register(
        FormatSegment1PropertyName,
        typeof(string),
        typeof(LocTextExtension),
        new UIPropertyMetadata(null));

    // ...
}

BaseLocalizeExtension类继承自MarkupExtension

代码语言:javascript
复制
public abstract class BaseLocalizeExtension<TValue> : MarkupExtension, IWeakEventListener, INotifyPropertyChanged

当我构建的时候,我得到了通常的"GetValue/SetValue does not exist in current context"错误。我试图让BaseLocalizeExtension类继承自DependencyObject,但是我得到了大量的错误。

有没有办法在MarkupExtension中使用xaml可绑定的DependencyProperty (或者也是可绑定的内容)?

谢谢你的提示

EN

回答 1

Stack Overflow用户

发布于 2012-03-02 00:36:25

你可以选择附加属性,在我看来,这是你唯一的选择。

例如:

代码语言:javascript
复制
public static readonly DependencyProperty FormatSegment1Property = DependencyProperty.RegisterAttached(
        "FormatSegment1", typeof(string), typeof(LocTextExtension), new PropertyMetadata(default(string)));

public static void SetFormatSegment1(DependencyObject element, string value)
{
    element.SetValue(FormatSegment1Property, value);
}

public static string GetFormatSegment1(DependencyObject element)
{
    return (string)element.GetValue(FormatSegment1Property);
}

代码语言:javascript
复制
<Window Title="{lex:LocText MyApp:Resources:windowTitle}" lex:LocText.FormatSegment1="{Binding Version}" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9520030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档