-2

I have a XAML file which is not show here. How do I to set the 'Visibility' of a field to 'Hidden' or 'Visible' based on the results of the OsCheck class method? Basically, I want the field Visibility to be 'Hidden' if the windows version is 7.

namespace My.namespace.is.secret
{
  public partial class MyClass: IValueConverter
  {
   public bool OsCheck(){
    System.OperatingSystem os = System.Environment.OSVersion;
    //Get version information about the os.

     System.Version vs = os.Version;

        if ((os.Platform == PlatformID.Win32NT) &&
            (vs.Major == 6) &&
            (vs.Minor != 0))
        {
             return true; //operatingSystem == "7";
        }

        else return false;

       }
    }
}
ConfusedDeer
  • 3,335
  • 8
  • 44
  • 72
  • 2
    Please provide [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) clearly illustrating your question. The code you posted won't even compile, as you haven't implemented the declared interface. The correct approach is to expose the check as a dependency property, and then bind it to your `Visibility` property via a converter, but you haven't shown any detail describing your scenario. See https://stackoverflow.com/q/21951023 for inspiration. – Peter Duniho Sep 11 '15 at 22:07

1 Answers1

0

You'd need to implement the Convert method, returning a visibility object based on your "OsCheck()":

namespace Mynamespace.issecret
{
  public class MyClass: IValueConverter
  {
       public bool OsCheck()
       {
           System.OperatingSystem os = System.Environment.OSVersion;
           //Get version information about the os.
           System.Version vs = os.Version;

           if ((os.Platform == PlatformID.Win32NT) &&
               (vs.Major == 6) &&
               (vs.Minor != 0))
           {
               return true; //operatingSystem == "7";
           }

           return false;
       }

      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          return OsCheck() ? Visibility.Collapsed : Visibility.Visible;
      }

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
}

Then in the XAML you'd use the converter like so:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:issecret="clr-namespace:Mynamespace.issecret"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <issecret:MyClass x:Key="OSCheck" />
        </Grid.Resources>
        <TextBlock Text="This is not Windows 7" Visibility="{Binding Converter={StaticResource OSCheck}}" />
    </Grid>
</Window>

Note that your OSCheck method will also return true for Window 8/8.1 and Server 2008R2-2012: https://en.wikipedia.org/wiki/Windows_NT

tyler
  • 59
  • 3