locked
How to set the Visibility of an control from Resources.resx file? RRS feed

  • Question

  • Hi,

    I'm trying to get the visibility value of a button from the resources.resx file. I'm getting error as "Cannot convert the string value to Visibility.Collapsed" . Here my code as follows,

    XAML Code:-

    <Button Margin="64,110,69,123" Name="button2" Content="{x:Static properties:Resource1.MainButtonContent}" Visibility="{x:Static properties:Resource1.MainButtonVisibility}" />

    Resources.resx Code:-

    Name                    Value
    MainButtonVisibility Collapsed 

    In the Output window the control should not appear, as per the above code. But the button is appearing in the window. As well as in the above code i have done for the Content of the Button its working fine for me.

     But the Visibility is not working, or am I did any mistakes on the code. Please correct me if I'm wrong.
     I have tried so many ways to resolve the above issue. but I couldn't Please any can give me the guidence or code samples would be greatly appericiate.

    Please let me know if more inputs required for the same.

    Thanks and Best Regards,
    Karthikeyan Manickam.

    Tuesday, December 16, 2008 10:21 AM

Answers

  •  Hi..What you are doing is setting a string valu to the Visibility attribute, which will result in this error.
    Here you should use Converters.

    Try something like this:

    <local:StringToVisibilityConverter x:Key="StringToVisibilityConverter" />

    <Button Margin="64,110,69,123" Name="button2" Binding="{Binding Path=Resource1.MainButtonContent}" Visibility="{Converter={StaticResource StringToVisibilityConverter}}" />

    Resources.resx Code:-

    Name                    Value
    MainButtonVisibility Collapsed 

    The purpose of a converter is to Convert one type into another type based on the desired mapping.

    [ValueConversion( typeof( string ), typeof( Visibility) )]
    public class StringToVisibilityConverter: IValueConverter
    {
    object IValueConverter.Convert(
        object value, Type targetType, object parameter, CultureInfo culture )
    {
        string str = (string)value;
        if (str=="Collapsed")
            return Visibility.Collapsed;
        //Whatever Processing.... 
    }

    object IValueConverter.ConvertBack(
        object value, Type targetType, object parameter, CultureInfo culture )
    {
      throw new NotSupportedException( "ConvertBack not supported." );
    }
    }

    HTH

    Get back if any issues.

    Saturday, December 27, 2008 6:47 AM

All replies

  •  Hi..What you are doing is setting a string valu to the Visibility attribute, which will result in this error.
    Here you should use Converters.

    Try something like this:

    <local:StringToVisibilityConverter x:Key="StringToVisibilityConverter" />

    <Button Margin="64,110,69,123" Name="button2" Binding="{Binding Path=Resource1.MainButtonContent}" Visibility="{Converter={StaticResource StringToVisibilityConverter}}" />

    Resources.resx Code:-

    Name                    Value
    MainButtonVisibility Collapsed 

    The purpose of a converter is to Convert one type into another type based on the desired mapping.

    [ValueConversion( typeof( string ), typeof( Visibility) )]
    public class StringToVisibilityConverter: IValueConverter
    {
    object IValueConverter.Convert(
        object value, Type targetType, object parameter, CultureInfo culture )
    {
        string str = (string)value;
        if (str=="Collapsed")
            return Visibility.Collapsed;
        //Whatever Processing.... 
    }

    object IValueConverter.ConvertBack(
        object value, Type targetType, object parameter, CultureInfo culture )
    {
      throw new NotSupportedException( "ConvertBack not supported." );
    }
    }

    HTH

    Get back if any issues.

    Saturday, December 27, 2008 6:47 AM
  • Hi Dev,

    Thanks dude. I have done with my work and I already figured it out the issues.

    As per the code posted by u, the same way I have tried it was worked properly.

    The code u have mentioned is the exact answer what I'm looking for.

     

    Thank u very much.

    Karthikeyan Manickam.

    Monday, December 29, 2008 6:16 AM