我有一个XAML文件,这里没有显示。如何根据OsCheck类方法的结果将字段的“可见性”设置为“隐藏”或“可见”? 基本上,如果Windows版本为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;
}
}
}
答案 0 :(得分:0)
您需要实现Convert方法,根据您的" 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();
}
}
}
然后在XAML中你会像这样使用转换器:
<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>
请注意,对于Window 8 / 8.1和Server 2008R2-2012,您的OSCheck方法也将返回true:https://en.wikipedia.org/wiki/Windows_NT