I currently have a TabControl loaded in MainWindow.xaml, which has three tabs. Let's name them Tab1, Tab2 and Tab3.
The individual tabs have views (usercontrols) supplied and they all work as expected. Code as follows. I am using MahApps Metro TabControl here.
<Controls:MetroAnimatedSingleRowTabControl Grid.Row="0" Grid.ColumnSpan="4" x:Name="MainTabControl">
<TabItem Header="Tab1">
<view:Tab1View DataContext="{Binding}" />
</TabItem>
<TabItem Header="Tab2">
<view:Tab2View DataContext="{Binding}" />
</TabItem>
<TabItem Header="Tab3">
<view:Tab3View DataContext="{Binding}" />
</TabItem>
</Controls:MetroAnimatedSingleRowTabControl>
What I would like to do now is to switch the view of Tab3 (which is Tab3View.xaml) to another view (let's call it subTab3View.xaml) when I click on a button on Tab3View.xaml. This will basically switch the Tab3 content from Tab3View.xaml to subTab3View.xaml.
Could anyone kindly suggest me a way to achieve this?
You can have a content control in your tab, and then change the content on button click or whatever event you want.
<TabItem Header="Tab3">
<ContentControl x:Name="contentControl"/>
</TabItem>
private void ButtonClick(object sender, RoutedEventArgs e)
{
this.contentControl.Content = new Tab3View();
}
Related
I have the following Tabcontrol definition:
<TabControl Grid.Row="0" SelectedItem="{Binding SelectedTab}" >
<TabItem Header="Foo" IsSelected="True" >
One
</TabItem>
<TabItem Header="Bar">
Two
</TabItem>
<TabItem Header="Smeh">
Three
</TabItem>
</TabControl>
I would expect the "Foo" tab to be selected and the 'One' text to be visible. This is not the behaviour I'm seeing. It appears all tabs are unselected and until I click on a different tab (clicking on the one that is supposedly already selected does nothing), then the tab looks selected and shows content.
Does anyone know why the TabControl works like this and how to fix it to work correctly?
By default there is no selected item in the TabControl. If you want to synchronize some model with the selected item, try
<TabControl Grid.Row="0" SelectedItem="{Binding SelectedTab, Mode=OneWayToSource}" >
I've got a rather strange behavior on a TabControl, whose TabItems are all collapsed: The content of the first TabItem is still visible (but the header is not).
The TabControl and its TabItems are setup like this:
<TabControl>
<TabItem Header="Data 1"
Visibility="{Binding Path=DataTable1.HasRows,
Converter={StaticResource BoolToVisibility}}">
<UI:ShowData DataContext="{Binding Path=DataTable1}"/>
</TabItem>
<TabItem Header="Data 2"
Visibility="{Binding Path=DataTable2.HasRows,
Converter={StaticResource BoolToVisibility}}">
<UI:ShowData DataContext="{Binding Path=DataTable2}"/>
</TabItem>
</TabControl>
If none of the data tables contains any rows, none of the TabItems should be shown. (I known that I could hide the whole TabControl in that case, but that's not the point here.)
Actually the content of the tab item "Header 1" will be displayed despite the TabItem being collapsed! The TabItem's header itself is collapsed, the TabItems border which contains its content and the content itself are displayed.
Edit/Add:
This can easily be reproduced using this code (note using Collapsed or Hidden does not make any difference:
<TabControl>
<TabItem Header="Test 1" Visibility="Hidden">
<Label>Test1</Label>
</TabItem>
<TabItem Header="Test 2" Visibility="Hidden">
<Label>Test2</Label>
</TabItem>
</TabControl>
So what's wrong here? Any help/hints are appreciated!
Ok, so you've found a real problem here... I looked around online and found several posts that relate to this. Some say that this is a bug, while others say that it is the designed behaviour. don't know which, although it certainly seems to be more of a bug than a feature.
Either way, you want to know how to deal with the problem. .. there are several solutions. One is just to set the TabItem.Content to null whenever you want to hide the tab and another is another involves adding an empty TabItem and selecting that item before hiding (so that it's empty content is shown).
You can attach a handler to the TabItem.IsVisibleChanged Event to be notified when the Visibility property has been changed:
public void TabItemIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
// Hide TabItem.Content here
}
Here are some links to the relevant posts:
Bug in TabControl/TabItem`s content visibility?
WPF TabControl - Select different tab when TabItem Visibility changes
Is there a workaround for this tabcontrol/tabitem bug
Another solution that I prefer over the ones suggested: Bind the visibility of the TabItem and its content to the same property (using the BooleanToVisibilityConverter).
Here's a simple example:
<UserControl.Resources >
<BooleanToVisibilityConverter x:Key="boolToVis"/>
</UserControl.Resources>
<Grid>
<TabControl>
<TabItem Header="TabItem 1" Visibility="{Binding Item1Visibility, Converter={StaticResource boolToVis}}">
<Label Content="Content 1" Visibility="{Binding Item1Visibility, Converter={StaticResource boolToVis}}"/>
</TabItem>
<TabItem Header="TabItem 2" Visibility="{Binding Item1Visibility, Converter={StaticResource boolToVis}}">
<Label Content="Content 2" Visibility="{Binding Item1Visibility, Converter={StaticResource boolToVis}}"/>
</TabItem>
</TabControl>
</Grid>
Could be a WPF bug, anyway bypass by binding the content visibility to the tab item visibility.
<TabControl>
<TabItem x:Name="_test1Tab" Header="Test 1" Visibility="Hidden">
<Label Visibility="{Binding ElementName=_test1Tab, Path=Visibility}">Test1</Label>
</TabItem>
<TabItem x:Name="_test2Tab" Header="Test 2" Visibility="Hidden">
<Label Visibility="{Binding ElementName=_test1Tab, Path=Visibility}">Test2</Label>
</TabItem>
</TabControl>
My solution to this was to put the TabItem I wanted to hide in another position. The problem happens only if you want to collapse only the first TabItem.
I have a TabControl with two TabItems, inside the one TabItem I have a DataGrid.
I'm trying to handle the TabItem click, and it works, but, when I click in one row of the "dataGrid1" the event "TabItem_MouseLeftButtonUp" of TabItem click is fired too. See the code:
<TabControl Height="211" HorizontalAlignment="Left" Margin="33,29,0,0" Name="tabControl1" VerticalAlignment="Top" Width="417" >
<TabItem Header="tabItem1" Name="tabItem1">
<Grid />
</TabItem>
<TabItem MouseLeftButtonUp="TabItem_MouseLeftButtonUp">
<DataGrid AutoGenerateColumns="True" Height="134" Name="dataGrid1" Width="307" />
</TabItem>
</TabControl>
Note: I can't use the personalize <TabItem.Header> because I'm using MahApps, if I use TabItem.Header the style os TabItem will break.
The MouseLeftButtonUp event is bubbling routed event. When you on the DataGrids row the event bubbling through its ancestors and calls the corresponding handlers, TabItem_MouseLeftButtonUp for TabItem in your case.
In your TabItem_MouseLeftButtonUp event you can check who raised the event, which control is the origin. If its not the TabItem do nothing.
private void TabItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if(sender is TabItem)
{
//do the work
}
}
You will recieve the EventArgs with MouseLeftButtonUp event. Just filter out whatever you need.
I am currently working in wpf. I was animating the color of textblock control inside the tab item. When the tab is selected, i want to change the foreground to white, i-e
<ColorAnimation
Storyboard.TargetName="buttonText"
Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
To="White" Duration="0:0:.1" />
the tab control code is;
<TabControl Name="tabSteps" Template="{StaticResource OfficeTabControl}">
<TabItem Header="Info" IsSelected="True">
<TextBlock>Info content</TextBlock>
</TabItem>
<TabItem Header="Recent">
<TextBlock>Recent content tab</TextBlock>
</TabItem>
<TabItem Header="New">
<TextBlock>New content tab</TextBlock>
</TabItem>
<TabItem Header="Print">
<TextBlock>Print content tab</TextBlock>
</TabItem>
<TabItem Header="Save & Send">
<TextBlock>Save & send content tab</TextBlock>
</TabItem>
<TabItem Header="Help">
<TextBlock>Help tab</TextBlock>
</TabItem>
</TabControl>
I want to target the textblock.
But it isn't working.
Any help will be appreciated.
I am following this work
http://www.codeproject.com/Articles/155211/Building-a-control-template-style-for-the-tabContr.aspx
Using the VisualStateManager, create a state for when your TabItem is selected. You can then specify the animation you mentioned to start as a transition to and from the selected state (see http://blogs.intuidev.com/post/2010/01/26/TabControlStyling_PartTwo.aspx for a tutorial).
The default style for the TabControl is here (http://msdn.microsoft.com/en-us/library/cc645035(v=vs.95).aspx), or you can easily generate it if you use Expression Blend.
You could also use this approach (a programmatic trigger) : https://stackoverflow.com/a/4958562/914602
How to set the background of TabItem? I tried the following code:
<TabControl>
<TabItem Header="Test" Background="Blue" Foreground="Red" />
</TabControl>
Foreground works, but Background does not work.
Any ideas? Thanks
What is happening is that in the case of a single tab, it is always selected, and so you are only seeing the selection style of the tab item.
For example, take a look at the following TabControl:
<TabControl>
<TabItem Header="Tab A" Background="Blue" Foreground="Red">
<Grid />
</TabItem>
<TabItem Header="Tab B" Background="Green" Foreground="Navy" >
<Grid />
</TabItem>
<TabItem Header="Tab C" Background="LightBlue">
<Grid />
</TabItem>
</TabControl>
Tab A will not display its Blue background until you select a different tab. If you truly want the Background to remain the same regardless of whether it is selected or not, you will need to override the control template of the TabItem.
See the question TabItem Background color changes when tabitem selected or hover over for an example of how to do this.