How do you add content to a tab control dynamically in wcf -- at run time?
How do you add content to a tab control dynamically in wcf -- at run time? I mean add content to the display area that comes visibile when clicking on the tab control.
Re: How do you add content to a tab control dynamically in wcf -- at run time?
very simple example. this simply adds a new TextBlock to the selected tab each time the tab is changed...
XAML
Code:
<Window x:Class="WpfApplication6.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">
<Grid>
<TabControl
Height="212"
HorizontalAlignment="Left"
Margin="58,35,0,0"
Name="tabControl1"
VerticalAlignment="Top"
Width="332"
SelectionChanged="tabControl1_SelectionChanged">
<TabItem
Header="tabItem1"
x:Name="tabItem1" >
<Grid>
<StackPanel
x:Name="tab1StackPanel"
Orientation="Vertical">
</StackPanel>
</Grid>
</TabItem>
<TabItem
Header="tabItem2"
x:Name="tabItem2">
<Grid>
<StackPanel
x:Name="tab2StackPanel"
Orientation="Vertical">
</StackPanel>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
Code:
Code:
public partial class MainWindow : Window
{
int counter = 1;
public MainWindow()
{
InitializeComponent();
}
private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tc = e.OriginalSource as TabControl;
TextBlock tb = new TextBlock();
tb.Width = 100;
tb.Height = 30;
tb.Text = counter.ToString();
if (tc.SelectedIndex == 0)
{
tab1StackPanel.Children.Add(tb);
}
else
{
tab2StackPanel.Children.Add(tb);
}
counter++;
}
}
Re: How do you add content to a tab control dynamically in wcf -- at run time?
I am curious as to why you use a safe cast instead of an explicit cast when you are not che3cking for null afterward? Just wondering.
Re: How do you add content to a tab control dynamically in wcf -- at run time?
Quote:
Originally Posted by
BigEd781
I am curious as to why you use a safe cast instead of an explicit cast when you are not che3cking for null afterward? Just wondering.
It bugs me just a little when I see people do that too. I guess it just comes down to which exception you want to hunt down null object or InvalidCast, either one provide about the same information I guess, but I prefer getting an InvalidCast if I made an invalid cast. Actually for most UI callbacks I use the as operator and then if it is null I return doing nothing but writing an error into the application log. My theory is whatever the user was trying to do was not going to work out to well anyway so I might as well just do nothing.
Re: How do you add content to a tab control dynamically in wcf -- at run time?
Quote:
Originally Posted by
BigEd781
I am curious as to why you use a safe cast instead of an explicit cast when you are not che3cking for null afterward? Just wondering.
because this is sample code and not something going into a production application. When I post code samples, I normally let the people I am doing it for deal with catching exceptions. Do I have to do everything for them?
I also don't have the time to catch every single exception that might occur when I am simply giving them sample code that will do what they need it to do.