WPF附加属性

时间:2011-02-02 15:58:33

标签: wpf localization attached-properties

我正在尝试实现设计时间本地化我希望Xaml窗口上的属性选择语言和语言控件,以自动更改其标签以使用适当的语言显示标签。

通过在LanguageLabel上检索包含语言符号(例如LANG_SURNAME或LANG_FORENAME)的附加属性(LanguageMarkupProperty)的内容来查找标签的文本,然后以指定的语言查找。到目前为止,我已经创建了一个从内容控件派生的对象:

public class LanguageLabel : ContentControl
{
    public LanguageLabel()
    {
        EventManager.RegisterClassHandler(typeof(Window),
                                            DisplayLanguage.LanguageChangedEvent,
                                            new RoutedEventHandler(this.ChangeLanguage));

        this.Loaded += new RoutedEventHandler(LanguageLabel_Loaded);
    }

    ~LanguageLabel()
    {
    }

    // Walk up the visual tree to find the top level object.
    private DependencyObject FindUpVisualTree(DependencyObject initial) 
    {
        DependencyObject current = initial;
        DependencyObject last = current;

        while (current != null)
        {
            last = current;
            current = VisualTreeHelper.GetParent(current);
        }

        return last;
    }

    void LanguageLabel_Loaded(object sender, RoutedEventArgs e)
    {
        object root = FindUpVisualTree(this);

        UIElement window = root as UIElement;
        PossibleLanguages ?language = null;
        if (window != null)
            language = (PossibleLanguages)window.GetValue(DisplayLanguage.DisplayLanguageProperty);
        if (language == null) language = PossibleLanguages.UKEnglish;

        string languageSymbol = GetValue(LanguageMarkup.LanguageMarkupProperty) as string;

        this.Content = Language.Translate(languageSymbol, language);

    }

    public void ChangeLanguage(object sender, RoutedEventArgs e)
    {
        LanguageChangedEventArgs le = e as LanguageChangedEventArgs;

        string languageSymbol = GetValue(LanguageMarkup.LanguageMarkupProperty) as string;

        this.Content = Stanley.Language.Translate(languageSymbol, (PossibleLanguages)le.DependencyPropertyChangedEventArgs.NewValue);
    }


}

我还创建了一个名为display language的附加属性,我将它放在Xaml中的Window上。

public class DisplayLanguage
{
    public static readonly DependencyProperty DisplayLanguageProperty;

    public static void SetDisplayLanguage(UIElement element, PossibleLanguages value)
    {
        element.SetValue(DisplayLanguageProperty, value);
    }

    public static PossibleLanguages GetDisplayLanguage(UIElement element)
    {
        return (PossibleLanguages)element.GetValue(DisplayLanguageProperty);
    }

    static DisplayLanguage()
    {
        DisplayLanguageProperty = DependencyProperty.RegisterAttached(
                             "DisplayLanguage",
                             typeof(PossibleLanguages),
                             typeof(WindowBase),
                             new FrameworkPropertyMetadata(PossibleLanguages.UKEnglish, FrameworkPropertyMetadataOptions.AffectsRender ,new PropertyChangedCallback(OnDisplayLanguageChanged))
                             );

    }


    public static void AddReceiver(object receiver, RoutedEventHandler e)
    {
    }

    public static void OnDisplayLanguageChanged(DependencyObject control, DependencyPropertyChangedEventArgs e)
    {
        // Fire an event to tell the LanguageLabel controls to update to the new language.
    }

}

我希望OnDisplayLanguageChanged以某种方式引发一个事件,告诉所有语言控件在设计时更新它们的显示。有谁知道我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以使用EventAggregator in PrismMessanger in MVVMLight之类的内容来广播所有语言控件都可以订阅的事件。

如果您不想使用这些框架,可以很容易地创建自己的模式实现。