![](/img/trans.png)
[英]MarkupExtension in ResourceDictionary.Source property
[英]MarkupExtension: Converting a simple property to DependencyProperty
我正在使用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(或者也可綁定的東西)?
謝謝你的提示
你可以選擇附屬房產,這是你看到的唯一選擇。
例如
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}" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.