C# WPF Set Focus TabItem - c#

I have a TabControl and i want to set the focus on the firsttab on a Textbox.
I tried to use in Code:
this.tabItemMerkmalAllgemein.Focus();
this.textBoxMerkmalBezeichnung.Focus();
If the second tab was selected the first Tab will be selected after this but no Cursor at the Textbox is shown, so the Textbox focus dosen't work.
XAML TabControle:
<TabControl Width="359" Grid.Column="1" Style="{StaticResource EditDialogTabControl}" Name="tabControlMerkmalDetails" Margin="4,0,0,0">
<TabItem Header="Allgemein" Name="tabItemMerkmalAllgemein" GotFocus="tabItemMerkmalAllgemein_GotFocus">
…
<Label Grid.Row="0" Style="{StaticResource EditDialogLabel}" Content="Bezeichnung"/>
<TextBox x:Name="textBoxMerkmalBezeichnung" Grid.Row="0" Grid.Column="1"
Style="{StaticResource EditDialogTextBox}" MaxWidth="300"
Text="{Binding Path=bezeichnung, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextChanged="textBoxMerkmalBezeichnung_TextChanged" LostFocus="textBoxMerkmalBezeichnung_LostFocus" />
…
</TabItem>
<TabItem Name="tabItemKostenpflichtig">
…
</TabItem>
</TabControl>
</Grid>
</TabItem>

You need to change your declaration of the TabItem to this:
<TabItem FocusManager.FocusedElement="{Binding ElementName=textBoxMerkmalBezeichnung}"
Header="Allgemein" Name="tabItemMerkmalAllgemein" GotFocus="tabItemMerkmalAllgemein_GotFocus">
The important part is the addition of the FocusManager.FocusedElement attached property. You can find out more from the FocusManager.FocusedElement Attached Property page at MSDN.

Related

Easiest way to bind property from view1 to view2

View1.xaml
<TabControl x:Name="MainTab">
<TabItem Header="1"></TabItem>
<TabItem Header="2"></TabItem>
View2.xaml
<StatusBar>
<StatusBarItem Content="{Binding ElementName=MainTab, Path=SelectedItem.Header, Mode=OneWay}" />
</StatusBar>
And the Header name of TabItem we can see on StatusBar, it works when both controls are in same view. I guess something is wrong with binding.

Setting VerticalContentAlignment in my WPF TabItem changes both the text in the tab and the Control contained in the tab

<TabItem Header="Data Entry"
FontSize="40"
VerticalContentAlignment="Top"
HorizontalContentAlignment="Center"
VerticalAlignment="Center"
Height="75"
Width="300"
Background="#464646"
Foreground="White"
DataContext="{Binding Tabs[0]}">
<Controls:DataEntryUserControl />
</TabItem>
My TabItem is formed with the code above. I want the text in the tab ("Data Entry") to be aligned to the center of the tab, but I want the UserControl to be aligned to the top of the window. However, both of these alignments change when I set VerticalContentAlignment (e.g. in the code above, both of them are aligned to the top of their respective parents). The VerticalAlignment property doesn't change anything.
How can I accomplish this?
Define a HeaderTemplate where you specify the alignment of the header:
<TabItem Header="Data Entry"
FontSize="40"
Height="75"
Width="300"
Background="#464646"
Foreground="White"
DataContext="{Binding Tabs[0]}">
<TabItem.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" HorizontalAlignment="Center" />
</DataTemplate>
</TabItem.HeaderTemplate>
<Controls:DataEntryUserControl />
</TabItem>

WPF DataTemplate Binding

I discovered when using a ContentTemplate/DataTemplate in a WPF TabControl my Bindings will not work anymore.
I have set up a small example to illustrate:
<Window x:Class="HAND.BindingExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BindingExample" Height="506" Width="656"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<Grid>
<TabControl HorizontalAlignment="Left" Height="381" VerticalAlignment="Top" Width="608">
<TabItem Header="TabItem">
<Label Content="{Binding Path=myString}"/>
</TabItem>
<TabItem Header="TabItem">
<TabItem.ContentTemplate>
<DataTemplate>
<Label Content="{Binding Path=myString}"/>
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
</TabControl>
</Grid>
</Window>
Tab1 works as expected, Tab2 is empty.
the code behind:
using System.Windows;
namespace HAND
{
public partial class BindingExample : Window
{
public string myString { get; set; }
public BindingExample()
{
myString = "Hello Stackoverflow";
InitializeComponent();
}
}
}
You are using the ContentTemplate property incorrectly. From the ContentControl.ContentTemplate Property page on MSDN:
Gets or sets the data template used to display the content of the ContentControl.
Therefore, when setting this property, you also need to set the Content property to some sort of data source:
<TabControl>
<TabItem Header="TabItem">
<Label Content="{Binding Path=myString}"/>
</TabItem>
<TabItem Header="TabItem" Content="{Binding Path=myString}">
<TabItem.ContentTemplate>
<DataTemplate>
<Label Content="{Binding}" />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
</TabControl>
<TabItem Content="{Binding myString}" Header="TabItem">
<TabItem.ContentTemplate>
<DataTemplate>
<Label Content="{Binding}" />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
But just so you know, to bind a window on itself, is just not the way to go.
I don't know if you did that just for the example, but if not try and create a proper viewModel to bind your window on ;)

Grid-like WPF control

I'd like to implement a 'TabManager' control which can be instantiated much like a Grid in XAML. This looks like a fairly common task, but I couldn't seem to find a tutorial and I still don't want to reinvent the wheel, so I was wondering if anyone here had an example.
I'd want the XAML to generate the control to look something like this:
<TabManager>
<TabManager.TabDefinitions>
<TabDefinition Caption="Tab 1"/>
<TabDefinition Caption="Tab 2"/>
<TabDefinition Caption="Tab 3"/>
</TabManager.TabDefinitions>
<TabPanel TabManager.Tab="0">
<TextBlock Text="foo"/>
</TabPanel>
<TabPanel TabManager.Tab="1">
<TextBlock Text="bar"/>
</TabPanel>
<TabPanel TabManager.Tab="2">
<TextBlock Text="baz"/>
</TabPanel>
</TabManager>
The TabManager will render a few buttons and some elements to contain the TabPanels' children - e.g. place them into a DockPanel or whatever else I choose to do; shouldn't matter.
Have you tried <TabControl>?
For instance -
<TabControl>
<TabItem Header="Tab1" x:Name="Tab1">
<Grid>
<TextBlock Text="foo"/>
</Grid>
</TabItem>
<TabItem Header="Tab2" x:Name="Tab2">
<Grid>
<TextBlock Text="bar"/>
</Grid>
</TabItem>
<TabItem Header="Tab3" x:Name="Tab3">
<Grid>
<TextBlock Text="baz"/>
</Grid>
</TabItem>
</TabControl>

How to automatically size TabControl in a DockPanel - WPF

I have a simple WPF Forms app. I have a DockPanel as my root panel. The first child is a StackPanel that has some controls in it, then the second control is a TabControl. What I want, and the panel types can change all they want is for the TabControl to maintain the fill size of the window except for what the first StackPanel consumes. However no matter what I try the TabControl seems to change its size depending on whats inside it, not whats it is inside of.
<Window>
<DockPanel>
<StackPanel> </StackPanel>
<TabControl> </TabControl>
</DockPanel>
</Window>
Just set the HorizontalAlignment and VerticalAlignment properties of your TabControl to "Stretch":
<DockPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Margin="5">
<TextBlock Text="Hello" />
<TextBlock Text="World" />
</StackPanel>
<TabControl HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<TabItem Header="Small">
<TextBlock Text="Just Some Small Stuff" />
</TabItem>
<TabItem Header="Bigger">
<StackPanel>
<TextBlock Text="One line" />
<TextBlock Text="The next line" />
</StackPanel>
</TabItem>
</TabControl>
</DockPanel>

Categories

Resources