Localized Properties in UserControl

I have a problem with localizations UserControlin my Windows Phone 8 app on XAML and C #. Unfortunately, I did not find a useful solution to my problem, despite the fact that I tried to fix it all the weekend.

Problem:

I have a custom UserControlone that I use in MainPage.xaml. I tried to bind the localized string to IngredientName, but I get:

System.Windows.Markup.XamlParseException (Failed to assign property)

This is MyUserControl.xaml :

<UserControl x:Name="myuc">
        <TextBlock x:Name="txt_IngredientName />
</UserControl>

In the code behind ( MyUserControl.xaml.cs ), I use the following lines to set and retrieve values ​​in MainPage:

public string IngredientName
{
    get { return this.txt_ingredientName.Text; }
    set { this.txt_ingredientName.Text = value; }
}

In MainPage.xaml, I call UserControl as follows:

<local:MyUserControl IngredientName="Chocolate"/>

. :

IngredientName="{Binding Path=LocalizedResources.Chocolate, Source={StaticResource LocalizedStrings}}"

.

, .., . - , UserControl? - ?

+4
1

Binding DependencyProperty. MyUserControl:

public static readonly DependencyProperty IngredientNameProperty
    = DependencyProperty.Register(
        "IngredientName",
        typeof(string),
        typeof(MyUserControl),
        new PropertyMetadata(HandleIngredientNameChanged));

public string IngredientName
{
    get { return (string)GetValue(IngredientNameProperty); }
    set { SetValue(IngredientNameProperty, value); }
}

private static void HandleIngredientNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var control = (MyUserControl) d;
    control.txt_ingredientName.Text = (string) e.NewValue;
}
+4

Source: https://habr.com/ru/post/1529810/


All Articles