Search code examples
wpfxamlivalueconverter

How to access const string from ViewModel in XAML x:Array


I am using a converter for which I want to pass in 2 converter parameters. I have static strings in Converter, but I am not able to access it.

Problem Statement:

Want to access public static string STAR = "STAR"; in XAML x:Array.

XAML:

<UserControl.Resources>
    <local:CustomGridLengthConverter x:Key="CustomGridLengthConverter"/>
</UserControl.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition>
            <ColumnDefinition.Width>
                <Binding Path="CustomLength" Converter="{StaticResource CustomGridLengthConverter}">
                    <Binding.ConverterParameter>
                        <x:Array Type="{x:Type system:String}">
                            <system:String>AUTO</system:String>-->Working
                            <system:String>STAR</system:String>-->Working
                            <!--<local:CustomGridLengthConverter.STAR/>--> *NOT* Working
                        </x:Array>
                    </Binding.ConverterParameter>
                </Binding>
            </ColumnDefinition.Width>
        </ColumnDefinition>
  </Grid.ColumnDefinitions>

Converter Code:

public class CustomGridLengthConverter : IValueConverter
    {
        public static string ABSOLUTE = "ABSOLUTE";
        public static string AUTO= "AUTO";
        public static string STAR = "STAR";

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
           double tempGridLengthVal = 0.0;

           //do necessary null/datatype check
           string[] params=parameter as string[];

           // use default value when 'value' is null
           string defaultValue=params[0];

           //use this value to specify if user wants absolute value or relative value.
           string convertTo=params[1];

           if (value.IsNotNull() && Double.TryParse(value.ToString(), out 
           tempGridLengthVal))
           {
            //If given value is parsable, create new GridLength with this value
            gridLength = new GridLength(tempGridLengthVal);

            //User can specify if they want to use the given value as exact 
             value or as a star percentage
            if (convertTo.Equals(STAR))
                gridLength = new GridLength(tempGridLengthVal,GridUnitType.Star);
            } 
        }

EDIT: This is a different use case than "another question" as in that question value is assigned to a dependency object, whereas I do not have a dependency object, I just want to pass in a static string value to an array.

Thanks,

RDV


Solution

  • You could use the x:Static markup extension:

    <Binding.ConverterParameter>
        <x:Array Type="{x:Type system:String}">
            <system:String>AUTO</system:String>
            <system:String>STAR</system:String>
            <x:Static Member="local:CustomGridLengthConverter.STAR" />
        </x:Array>
    </Binding.ConverterParameter>