질문하기질문하기
 

답변됨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.