【问题标题】:MarkupExtension: Converting a simple property to DependencyPropertyMarkupExtension:将简单属性转换为 DependencyProperty
【发布时间】:2012-03-01 16:28:39
【问题描述】:

我正在使用 WPFLocalizationExtension(在 CodePlex 上可用)在我的 WPF 应用程序中本地化字符串。这个简单的 MarkupExtension 在像这样的简单场景中效果很好:

<Button Content="{lex:LocText MyApp:Resources:buttonTitle}" />

但是当我尝试一些更复杂的事情时,我就被卡住了:

<Window Title="{lex:LocText MyApp:Resources:windowTitle, FormatSegment1={Binding Version}}" />

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

由于 FormatSegment1 是一个普通的 INotifyPropertyChange 属性,我无法将任何内容绑定到它。如果 FormatSegment1 是一个 DependencyProperty 是可能的,所以我下载了源代码并尝试修补它们。

我修改了

[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

public abstract class BaseLocalizeExtension<TValue> : MarkupExtension, IWeakEventListener, INotifyPropertyChanged

当我构建时,我得到了通常的"GetValue/SetValue does not exist in current context" 错误。我尝试让BaseLocalizeExtension 类继承自DependencyObject,但我遇到了很多错误。

有没有办法在 MarkupExtension 中使用 xaml 可绑定的 DependencyProperty(或者也可以绑定的东西)?

谢谢你的提示

【问题讨论】:

标签: c# wpf xaml markup-extensions wpflocalizationextension


【解决方案1】:

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

例如

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);
}

.

<Window Title="{lex:LocText MyApp:Resources:windowTitle}" lex:LocText.FormatSegment1="{Binding Version}" />

【讨论】:

  • 我尝试了您的解决方案,但在设计器中出现 NullRefException:System.NullReferenceException 对象引用未设置为对象实例。在 MS.Internal.Design.Markup.MarkupExtensionParser.ParseNamedArguments(TypeNode 类型,List`1 个参数)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
相关资源
最近更新 更多