我正在使用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(或者也可绑定的东西)?
谢谢你的提示
答案 0 :(得分:0)
你可以选择附属物,这是你看到的唯一选择。
e.g。
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}" />