我的观点看起来像
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
</Window.Resources>
<DockPanel>
<TabControl Grid.Row="0" Grid.Column="0" >
<TabItem Header="TabItem1">
<Grid x:Name="Grid1" Background="LightBlue" Visibility="{Binding Grid1Visiblity, Mode=TwoWay, Converter={StaticResource BoolToVisibilityConverter}}" IsVisibleChanged="Grid1_OnIsVisibleChanged"/>
</TabItem>
<TabItem Header="TabItem2" >
<Grid Background="LightCyan" >
</Grid>
</TabItem>
</TabControl>
</DockPanel>
后面的代码看起来像
public MainWindow()
{
InitializeComponent();
DataContext = this;
Grid1Visiblity = true;
}
private Boolean grid1Visiblity;
public Boolean Grid1Visiblity
{
get { return grid1Visiblity; }
set
{
grid1Visiblity = value;
OnPropertyChanged("Grid1Visiblity");
}
}
现在为什么当我从TabItem1切换到TabItem2时,我没有看到Visiblity setter,即Grid1Visbility设置?
当我使用IsVisibilityChanged事件处理程序时,我看到可见性发生了变化,从MSDN文档中,只有当可见性发生变化时才会触发IsVisibilityChanged事件,那么为什么绑定在我的情况下不起作用。