提出问题提出问题
 

已答复How to set the Visibility of an control from Resources.resx file?

  • 2008年12月16日 10:21KarthikeyanManickam 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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.

答案

  • 2008年12月27日 6:47Dev_DotNet 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
     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.

全部回复

  • 2008年12月27日 6:47Dev_DotNet 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
     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.

  • 2008年12月29日 6:16KarthikeyanManickam 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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.