Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MarkupExtension:将简单属性转换为DependencyProperty_C#_Wpf_Xaml_Markup Extensions_Wpflocalizationextension - Fatal编程技术网

C# MarkupExtension:将简单属性转换为DependencyProperty

C# MarkupExtension:将简单属性转换为DependencyProperty,c#,wpf,xaml,markup-extensions,wpflocalizationextension,C#,Wpf,Xaml,Markup Extensions,Wpflocalizationextension,我正在使用WPFLocalizationExtension(在上提供)来本地化WPF应用程序中的字符串。此简单的MarkupExtension在以下简单场景中运行良好: <Button Content="{lex:LocText MyApp:Resources:buttonTitle}" /> BaseLocalizeExtension类继承自MarkupExtension: public abstract class BaseLocalizeExtension<TValue

我正在使用WPFLocalizationExtension(在上提供)来本地化WPF应用程序中的字符串。此简单的MarkupExtension在以下简单场景中运行良好:

<Button Content="{lex:LocText MyApp:Resources:buttonTitle}" />
BaseLocalizeExtension
类继承自
MarkupExtension

public abstract class BaseLocalizeExtension<TValue> : MarkupExtension, IWeakEventListener, INotifyPropertyChanged
公共抽象类BaseLocalizeExtension:MarkupExtension、IWeakEventListener、INotifyPropertyChanged 当我构建时,我得到了通常的
“GetValue/SetValue在当前上下文中不存在”
错误。我试图使
BaseLocalizeExtension
类继承自
DependencyObject
,但我得到了大量错误

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


谢谢您的提示

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

e、 g



@jberger如果我从DependencyObject派生类,如何使其表现为MarkupExtension?我想你不能。你必须改变你的模式。您可以尝试Simon答案的修改版本。我尝试了您的解决方案,在设计器中得到一个NullRefException:System.NullReferenceException对象引用未设置为对象的实例。位于MS.Internal.Design.Markup.MarkupExtensionParser.ParseNamedArguments(类型节点类型,列出'1个参数)
[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));

    // ...
}
public abstract class BaseLocalizeExtension<TValue> : MarkupExtension, IWeakEventListener, INotifyPropertyChanged
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}" />