Change tab on button click event - wpf

i would like to change to a different tab when i click a button.
my xaml is as follow:
<TabControl Name="Tabcontrol1" Margin=" 5" SelectedIndex="0">
<TabItem Header="Properties" Opacity="1">
<Grid Width="1185" Height="945" Background="Snow" >
</Grid>
</TabItem>
<TabItem Header="Others">
<Grid>
</Grid>
</TabItem>
</TabControl>
and my button click event:
private void BuildButton_Click(object sender, RoutedEventArgs e)
{
Tabcontrol1.SelectedIndex = "1";
}
is there something wrong? "Cannot implicitly convert type 'string' to 'int'" appears

Remove quotes:
Tabcontrol1.SelectedIndex = 1;

Related

How to get previous selected TabItem Index from TabControl?

Following xaml code for your testing needs.
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="350">
<DockPanel>
<TabControl x:Name="TabControl" DockPanel.Dock="top">
<TabItem x:Name="TabItem1" Header="London">
<Label Content="London" />
</TabItem>
<TabItem x:Name="TabItem2" Header="Paris">
<Label Content="Paris" />
</TabItem>
<TabItem x:Name="TabItem3" Header="Tokyo">
<Label Content="Tokyo" />
</TabItem>
<TabItem x:Name="TabItem4" Header="Istanbul">
<Label Content="Istanbul" />
</TabItem>
</TabControl>
</DockPanel>
</Window>
How to get previous TabItem Index from TabControl and show in the MessageBox?
I need vb.net code running from code behind.
RemovedItems is an IList property in the SelectionChangedEventArgs that holds the items that were unselected since the last time the SelectionChanged event occurred.
You could check it each time the SelectionChanged of the TabControl event occurred:
private void TabControl_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0)
{
var oldTabItem = e.RemovedItems[0] as TabItem;
}
}
xaml:
<DockPanel>
<TabControl x:Name="TabControl" DockPanel.Dock="top" SelectionChanged="TabControl_OnSelectionChanged" > ..

How control re-position depends on other control visibility in same panel

I have two buttons inside a stack panel. Initially B1 button is on top, then B2. I will change button visibility dynamically from code so that, when I change B1 visibility hidden, then B2 will come on top. How can I achieve this functionality.
<Grid>
<StackPanel >
<Button Content="B1" Height="20" Width="100" Visibility="Visible"/>
<Button Content="B2" Height="20" Width="100" Visibility="Visible"/>
</StackPanel>
</Grid>
First you remove the Statckpanel and put then in a Grid and you can achieve
Try something like this.
<Grid>
<Button Content="B1" Height="20" Width="100" Visibility="Visible" Click="Button_Click" x:Name="B1" />
<Button Content="B2" Height="20" Width="100" Visibility="Visible" x:Name="B2" Click="B2_Click" />
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
B1.Visibility = System.Windows.Visibility.Hidden;
B2.Visibility = System.Windows.Visibility.Visible;
}
private void B2_Click(object sender, RoutedEventArgs e)
{
B2.Visibility = System.Windows.Visibility.Hidden;
B1.Visibility = System.Windows.Visibility.Visible;
}
This should give you similar behaviour. Change according to your use

How to make a collapsed tab item visible programmatically in WPF?

how do i make a collapsed tab item visible programmatically in WPF?
I have created a xaml file with a tabcontrol with three tabs, the visibility property I have set succesively to Visible. Hidden and Collapsed. Also I have two buttons one to set Tab 2 to visible and the other to set Tab 3 to visible.
In the code behind I have set up an event handler for each button.
The event handlers don't compile.
I would appreciate any suggestions . Thanks!
Here is my MainWindow.xaml
<Window x:Class="WPFTabItemVisibility.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TabControl>
<TabItem Header="Tab 1" Visibility="Visible" Content="Tab 1 Content goes here"></TabItem>
<TabItem Header="Tab 2" Visibility="Hidden" Content="Tab 2 Content goes here"></TabItem>
<TabItem Header="Tab 3" Visibility="Collapsed" Content="Tab 3 Content goes here"></TabItem>
</TabControl>
<Button Content="Make Tab 2 Visible" Margin="20" Height="30" Width="200" Click="Button_Click"/>
<Button Content="Make Tab 3 Visible" Margin="20" Height="30" Width="200" Click="Button_Click_1"/>
</StackPanel>
</Window>
here is my MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WPFTabItemVisibility
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TabIndex = 1;
TabItem.VisibilityProperty = IsVisible;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
TabIndex = 2;
TabItem.VisibilityProperty = IsVisible;
}
}
}
Thank you!
You can give name to the tabitem and set its visibility. Refer the below code.
<TabControl>
<TabItem x:Name="tab1" Header="Tab 1" Visibility="Visible" Content="Tab 1 Content goes here"></TabItem>
<TabItem x:Name="tab2" Header="Tab 2" Visibility="Hidden" Content="Tab 2 Content goes here"></TabItem>
<TabItem x:Name="tab3" Header="Tab 3" Visibility="Collapsed" Content="Tab 3 Content goes here"></TabItem>
</TabControl>
private void Button_Click(object sender, RoutedEventArgs e)
{
tab2.Visibility = Visibility.Visible;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
tab3.Visibility = Visibility.Visible;
}
try this in your XAML
<StackPanel>
<TabControl>
<TabItem Header="Tab 1"
Visibility="Visible"
Name="tab1"
Content="Tab 1 Content goes here"></TabItem>
<TabItem Header="Tab 2"
Visibility="Hidden"
Name="tab2"
Content="Tab 2 Content goes here"></TabItem>
<TabItem Header="Tab 3"
Name="tab3"
Visibility="Collapsed"
Content="Tab 3 Content goes here"></TabItem>
</TabControl>
<Button Content="Make Tab 2 Visible"
Margin="20"
Height="30"
Width="200"
Click="Button_Click" />
<Button Content="Make Tab 3 Visible"
Margin="20"
Height="30"
Width="200"
Click="Button_Click_1" />
</StackPanel>
CodeBehind .cs File
private void Button_Click(object sender, RoutedEventArgs e)
{
//TabIndex = 1;
//TabItem.VisibilityProperty = IsVisible;
tab2.Visibility = Visibility.Visible;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//TabIndex = 2;
//TabItem.VisibilityProperty = IsVisible;
tab3.Visibility = Visibility.Visible;
}

visibility binding does not work with tabitem

My view looks like
<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>
and the code behind looks like
public MainWindow()
{
InitializeComponent();
DataContext = this;
Grid1Visiblity = true;
}
private Boolean grid1Visiblity;
public Boolean Grid1Visiblity
{
get { return grid1Visiblity; }
set
{
grid1Visiblity = value;
OnPropertyChanged("Grid1Visiblity");
}
}
Now why is it that when I switch from TabItem1 to TabItem2 that I do not see Visiblity setter namely Grid1Visbility set?
I see that visibility changes when I use the IsVisibilityChanged event handler, from the MSDN docs the IsVisibilityChanged event is triggered only when visibility changes, so why does the binding does not work in my case.

How to access Button programatically if the button is inside using C#

I have few questions as I am usign XAML for the first time.
How do I use the button (BrowseButton) to browse a folder in Harddrive ?
In this case as the button is inside
Can I use the way I have shown below?
Actually first dockpanel will hold the image and some label and the other dockpanel will have tabcontrol in it.
If I have a tabcontrol, How can I add a listview which can increase the tabs during runtime.. and the listview should be available in the runtime also.
How to add a close("X") mark on the top of the tab to close the button
probably I asked a lot of questions, sorry :(
Please help
<Grid>
<DockPanel>
<StackPanel>
<Image Name="imgClientPhoto" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto" Height="auto" Grid.Column="0" Grid.Row="0" Margin="0"
Source="D:ehtmp_top_left.gif" MinWidth="450" MinHeight="100" Grid.IsSharedSizeScope="True">
</Image>
<Button x:Name="BrowseButton" Margin="0,13.638,30,14.362" Content="Browse" Click="BrowseButton_Click" HorizontalAlignment="Right" Width="111" />
<TextBox x:Name="txtBxBrowseTB" Margin="46,10,146,10" Text="TextBox" TextWrapping="Wrap" TextChanged="BrowseTB_TextChanged"></TextBox>
<Label HorizontalAlignment="Left" Margin="-14,22,0,10" Name="label1" Width="69.75" FontSize="13" VerticalContentAlignment="Top" HorizontalContentAlignment="Center">Path:</Label>
</Grid>
</GroupBox>
</Grid>
</StackPanel>
</DockPanel>
<DockPanel>
<StackPanel Margin="0,158,0,0" HorizontalAlignment="Left" Width="330" Height="557.5" VerticalAlignment="Top">
<TextBox Height="50" Name="textBox1" Width="240" Margin="0,35,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Right" />
<ListBox Height="470" Name="listBox1" Width="332" Background="LightGray" Margin="0,0,0,0" BorderBrush="IndianRed" BorderThickness="3" />
</StackPanel>
<TabControl Height="234" Name="tabControl1" Width="1035" Margin="0,0,0,0">
<TabItem Header="tabItem1" Name="tabItem1">
<Grid Height="144" />
</TabItem>
</TabControl>
</DockPanel>
</Grid>
1) The browse code should be something like this:
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.FolderBrowserDialog browse = new System.Windows.Forms.FolderBrowserDialog();
browse.RootFolder= Environment.SpecialFolder.MyDocuments;
browse.SelectedPath = "C:\\InitalFolder\\SomeFolder";
if (browse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtBxBrowseTB.Text = browse.SelectedPath;
}
}
2) I have tried to simplify the xaml. Does this look like something you could go with?:
<Grid>
<Image Name="imgClientPhoto" Margin="0" Height="262" VerticalAlignment="Top" HorizontalAlignment="Left" ></Image>
<StackPanel VerticalAlignment="Stretch" >
<StackPanel VerticalAlignment="Top" Orientation="Horizontal" Height="30">
<Button Name="BrowseButton" Content="Browse" Click="BrowseButton_Click" HorizontalAlignment="Right" Width="111" />
<Label Name="label1" Width="69.75" FontSize="13" VerticalContentAlignment="Center" HorizontalContentAlignment="Right">Path:</Label>
<TextBox Name="txtBxBrowseTB" Width="200" Text="TextBox" VerticalContentAlignment="Center" TextWrapping="Wrap" TextChanged="BrowseTB_TextChanged"></TextBox>
</StackPanel>
<StackPanel>
<StackPanel Orientation="Vertical">
<TextBox Height="50" Name="textBox1" />
<ListBox Height="470" Name="listBox1" Background="LightGray" Margin="0,0,0,0" BorderBrush="IndianRed" BorderThickness="3" MouseLeftButtonUp="listBox1_MouseLeftButtonUp">
<ListBoxItem>User1</ListBoxItem>
<ListBoxItem>User2</ListBoxItem>
<ListBoxItem>User3</ListBoxItem>
<ListBoxItem>User4</ListBoxItem>
</ListBox>
</StackPanel>
<TabControl Name="tabControl1" />
</StackPanel>
</StackPanel>
</Grid>
You can also have a close button in the tab item Header. As many of the content properties in xaml controls, the content can actually be controls, and not necessary just a text.
<TabControl Name="tabControl1" >
<TabItem Name="tabItem1" >
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<Label>TabHeader1</Label>
<Button Height="20" Width="20" FontWeight="Bold" Click="CloseTabButton_Click">X</Button>
</StackPanel>
</TabItem.Header>
<Grid Height="144">
<ListView />
</Grid>
</TabItem>
</TabControl>
3) Here is how you would add the tabs in C# dynamically:
private void listBox1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//Get selected item
ListBoxItem item = (ListBoxItem)listBox1.SelectedItem;
string itemText = item.Content.ToString();
//Check if item is already added
foreach (TabItem tItem in tabControl1.Items)
{
if ((string)tItem.Tag == itemText)
{
//Item already added, just activate the tab
tabControl1.SelectedItem = tItem;
return;
}
}
//Build new tab item
TabItem tabItem = new TabItem();
tabItem.Tag = itemText;
//First build the Header content
Label label = new Label();
Button button = new Button();
label.Content = itemText;
button.Content = "X";
button.Height = 20;
button.Width = 20;
button.FontWeight = FontWeights.Bold;
button.Click += CloseTabButton_Click; //Attach the event click method
button.Tag = tabItem; //Reference to the tab item to close in CloseTabButton_Click
StackPanel panel = new StackPanel();
panel.Orientation = Orientation.Horizontal;
panel.Children.Add(label);
panel.Children.Add(button);
tabItem.Header = panel;
//Then build the actual tab content
//If you need only the ListView in here, you don't need a grid
ListView listView = new ListView();
//TODO: Populate the listView with what you need
tabItem.Content = listView;
//Add the finished tabItem to your TabControl
tabControl1.Items.Add(tabItem);
tabControl1.SelectedItem = tabItem; //Activate the tab
}
private void CloseTabButton_Click(object sender, RoutedEventArgs e)
{
//The tab item was set to button.Tag when it was added
Button button = (Button)sender;
TabItem tabItem = (TabItem)button.Tag;
if (tabItem != null)
{
button.Click -= CloseTabButton_Click; //Detach event handler to prevent memory leak
tabControl1.Items.Remove(tabItem);
}
}

Resources