我不确定它在WPF和C#中是如何工作的。它仍然很容易混淆。当TabControl
或DataContext上的属性为true时,我想要显示x:Class
。我像这样将DataContext分配给我的窗口;
private ModelObject DataContextModel = new ModelObject()
{
Minutes = 0,
Hour = 0
};
...
this.DataContext = DataContextModel
ModelObject看起来像这样;
public class ModelObject : INotifyPropertyChanged
{
...
bool LiveScrapingValue = true;
public bool LiveScraping
{
get { return LiveScrapingValue; }
set
{
LiveScrapingValue = value;
}
}
...
我的TabControl元素如下所示;
<TabControl Name="ScraperLiveAttorneysTabControl" Visibility="{Binding LiveScraping, Converter={StaticResource boolToVisibilityConverter}}">
我知道我应该创建一个bool to visibility转换器,所以我在x:Class上创建了一个;
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
if ((bool)value == true)
{
return Visibility.Visible;
}
else
{
return Visibility.Hidden;
}
}
return Visibility.Hidden;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
..以及随之而来的资源;
<Controls:MetroWindow.Resources>
<local:BooleanToVisibilityConverter x:Key="boolToVisibilityConverter"/>
</Controls:MetroWindow.Resources>
现在我无法访问我设置的this.DataContext数据并将属性LiveScraping
更改为true或false以显示或隐藏TabControl。我的方法就是使用;
DataContextModel.LiveScraping = true;
但这不起作用,因为this.DataContext
并非指向DataContextModel