Problem while attempting to change selected index of a tabControl
The problem i face is quite annoying.
I have a tabControl with the 2 tabItems(Home,Show) The Home tab starts as Visible and Show as Hidden
In Home i have an image for which i handle the MouseLeftButtonDown event. This event should change the visibility of Show TabItem and make it Selected.
At the code i have a tabControl.SelectedIndex = 1; which forces the tabControl SelectionChangeEvent (which i use to change the foreground of the Show and Home TabItems).
The problem i face is that instead of the focus to get passed to Show, it remains on Home. I have no problem with the code, because at the last step of the SelectionChange event handler ,which is the last function that gets executed by my code, what i see in the ui is correct.
So what gets executed after that? Can anybody help?
private void Main_clientImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
开发者_StackOverflow社区 showMenu.Visibility = Visibility.Visible;
setTabSelected("showMenu");
}
public void setTabSelected(String name)
{
for (int i = 0; i tabControl.Items.Count ; i++)
{
TabItem item = tabControl.Items.GetItemAt(i) as TabItem;
if (item.Name.Equals(name))
{
selectedTab=i;
tabControl.SelectedIndex = i;
item.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
item.Foreground = new SolidColorBrush(Colors.White);
}
}
}
private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
for (int i = 0; i tabControl.Items.Count; i++)
{
TabItem item = tabControl.Items.GetItemAt(i) as TabItem;
if (tabControl.SelectedIndex == i)
{
item.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
item.Foreground = new SolidColorBrush(Colors.White);
}
}
}
//EDIT: I solved it by adding a simple e.Handler = true at MouseLeftButtonDown event. Whats wrong with this thing???
I changed the SelectedItem
of TabControl
in a method
void open_Click(object sender, RoutedEventArgs e)
and did it from a <Button Click="open_Click"
and from a <DataGrid MouseDoubleClick="open_Click"
.
Only by adding
e.Handled = true;
at the end of in method open_Click
it worked for the MouseDoubleClick
as Fotis mentioned in the question.
There is problem with your code.
private void Main_clientImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
showMenu.Visibility = Visibility.Visible;
homeMenu.Visibility = Visibility.Hidden;
setTabSelected("showMenu");
}
public void setTabSelected(String name)
{
for (int i = 0; i < tabControl.Items.Count; i++)
{
TabItem item = tabControl.Items.GetItemAt(i) as TabItem;
if (item.Header.Equals(name))
{
selectedTab = i;
item.IsSelected = true;
item.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
item.Foreground = new SolidColorBrush(Colors.White);
}
}
}
private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
for (int i = 0; i < tabControl.Items.Count; i++)
{
TabItem item = tabControl.Items.GetItemAt(i) as TabItem;
if (tabControl.SelectedIndex == i)
{
item.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
item.Foreground = new SolidColorBrush(Colors.White);
}
}
}
In the above code I am checking item.Header.Equals(name)
and set item.IsSelected = true
. Sometimes selectionindex doesn't work properly. The above code should address your problem.
After the SelectedItem property of the TabControl has changed, you need to make sure that SelectedItem.IsFocused = true. If not just use SelectedItem.Focus() right after you changed the SelectedItem property. It helped in my case.
精彩评论