MSDN > 論壇首頁 > Working with Data in WPF Applications in the Visual Studio 2010 and .NET Framework 4.0 CTP > How to set the Visibility of an control from Resources.resx file?
發問發問
 

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

  • Tuesday, 16 December, 2008 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.

解答

  • Saturday, 27 December, 2008 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.

所有回覆

  • Saturday, 27 December, 2008 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.

  • Monday, 29 December, 2008 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.