I have a tab control with three tabs, two of which are have Visibility set to collapsed when the program starts, becomes visible under certain conditions, and can later become collapsed again. If the TabItem being collapsed is the currently selected Tab, it's content remains visible even though it has become collapsed.
The tabs' visibility is bound to my ViewModel, and is updated that way.
It will always be the case that I want the first tab to be activated when any of the tab's visibility changes. I've tried to make a simple code behind to handle this case, but the only time that code is hit is when my UserControl is loaded/unloads. The Handler is never invoked when the tab's visibility is updated. I tried setting the IsVisibleChanged property on both the tabcontrol and its items, but I can't get the codebeind to hit.
Here's my xaml:
<UserControl x:Class="MyNameSpace.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
IsVisibleChanged="TabControl_IsVisibleChanged>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Platform.Presentation;component/Themes/MyTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<TabControl x:Name="_tabControl" IsVisibleChanged="TabControl_IsVisibleChanged">
<TabItem Header="View 1" x:Name="_view1Tab" IsVisibleChanged="TabControl_IsVisibleChanged">
<local:SingleWorkspaceView/>
</TabItem>
<TabItem Header="View 2" x:Name="_view2Tab" Visibility="{Binding TabVisibility}" IsVisibleChanged="TabControl_IsVisibleChanged">
<local:WorkspaceDeploymentView/>
</TabItem>
<TabItem Header="View 3" x:Name="_view3Tab" Visibility="{Binding TabVisibility}" IsVisibleChanged="TabControl_IsVisibleChanged">
<local:TabDeploymentView/>
</TabItem>
</TabControl>
Here's my code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MyNameSpace
{
/// <summary>
/// Interaction logic for TopLevelControl.xaml
/// </summary>
public partial class TopLevelControl : UserControl
{
ApplicationViewModel _viewModel;
public TopLevelControl()
{
_viewModel = new ApplicationViewModel();
base.DataContext = _viewModel;
InitializeComponent();
}
private void TabControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TabItem tab = sender as TabItem;
if(tab != null && (bool)e.NewValue)
{
_tabControl.SelectedIndex = 0;
}
}
}
}
Is there some reason the event is not firing?
Lets do it in MVVM way without any codebehind
xaml here we have binded SelectedIndex="{Binding TabSelectedIndex}" of TabControl
<TabControl SelectedIndex="{Binding TabSelectedIndex}">
<TabItem Header="abc">
<Button Content="ok"/>
</TabItem>
<TabItem Header="xyz" Visibility="{Binding TabVisibility}">
<Button Content="ok"/>
</TabItem>
<TabItem Header="pqr" Visibility="{Binding TabVisibility}">
<Button Content="ok"/>
</TabItem>
</TabControl>
xaml.cs
public MainWindow()
{
InitializeComponent();
DataContext =new ViewModel();
}
ViewModel
public class ViewModel: INotifyPropertyChanged
{
int tabSelectedIndex;
//this will be bound to the SelectedIndex of Tabcontrol
public int TabSelectedIndex
{
get { return tabSelectedIndex; }
set { tabSelectedIndex = value;
Notify("TabSelectedIndex");
}
}
Visibility tabVisibility;
//this will be binded to the Visibility of TabItem
public Visibility TabVisibility
{
get { return tabVisibility; }
set
{
tabVisibility = value;
//this is the logic that will set firstTab selected when Visibility will be collapsed
if (tabVisibility == Visibility.Collapsed)
{
tabSelectedIndex = 0;
Notify("TabSelectedIndex");
}
Notify("TabVisibility"); }
}
public event PropertyChangedEventHandler PropertyChanged;
void Notify(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
I hope this will help. If it will help then say MVVM Rocks :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With