How to pass static value to IValueConverter in XAML

I would like to use static texts retrieved from a web service in my WP7 application. Each text has a name (identifier) ​​and a Content property.

For example, the text may look like this:

Name = "M43"; Content = "This is the text to be shown"; 

I would like to pass the name (i.e. identifier) ​​of the text to IValueConverter , which will then look for the name and return the text.

I thought the converter looked something like this:

 public class StaticTextConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null) { return App.StaticTexts.Items.SingleOrDefault(t => t.Name.Equals(value)).Content; } return null; } } 

Then in XAML:

 <phone:PhoneApplicationPage.Resources> <Helpers:StaticTextConverter x:Name="StaticTextConverter" /> </phone:PhoneApplicationPage.Resources> ... <TextBlock Text="{Binding 'M43', Converter={StaticResource StaticTextConverter}}"/> 

However, this does not work, and I'm not sure if I pass the value to the converter correctly.

Does anyone have any suggestions?

+6
source share
4 answers

I finally found the answer. The answer was a confusion between @Shawn Kendrot and another question I asked here: IValueConverter does not start in some scenarios

To summarize the solution for using IValueConverter , I need to link my control to the following estate:

 <phone:PhoneApplicationPage.Resources> <Helpers:StaticTextConverter x:Name="TextConverter" /> </phone:PhoneApplicationPage.Resources> <TextBlock Text="{Binding Converter={StaticResource TextConverter}, ConverterParameter=M62}" /> 

Since the text identifier is passed with the converter parameter, the converter looks almost the same:

 public class StaticTextConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (parameter != null && parameter is string) { return App.StaticTexts.Items.SingleOrDefault(t => t.Name.Equals(parameter)).Content; } return null; } } 

However, as it turned out, the binding and, therefore, the converter is not called if it does not have a DataContext . To solve this problem, the DataContext property of the control simply needs to be set to something arbitrary:

 <TextBlock DataContext="arbitrary" Text="{Binding Converter={StaticResource TextConverter}, ConverterParameter=M62}" /> 

And then everything works as intended!

+11
source

If you want to use a value converter, you need to pass a string to the value converter parameter

Xaml:

 <TextBlock Text="{Binding Converter={StaticResource StaticTextConverter}, ConverterParameter=M43}"/> 

Converter

 public class StaticTextConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (parameter != null) { return App.StaticTexts.Items.SingleOrDefault(t => t.Name.Equals(parameter)).Content; } return null; } } 
+3
source

The problem is your binding. He will check the DataContext , and on this object he will try to evaluate the properties of the M62 and ValueboxConsent on this object.

You might want to add static keys somewhere in your application where you can bind to:

 <TextBlock Text="{Binding Source="{x:Static M62.ValueboxConsent}", Converter={StaticResource StaticTextConverter}}" /> 

Where M62 is the static class where your keys are located .. for example:

 public static class M62 { public static string ValueboxConsent { get { return "myValueBoxConsentKey"; } } } 
+2
source
 xmlns:prop="clr-namespace:MyProj.Properties;assembly=namespace:MyProj" <TextBlock Text="{Binding Source={x:Static prop:Resources.MyString}, Converter={StaticResource StringToUpperCaseConverter}}" /> 
+1
source

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


All Articles