how to get Tabcontrol's Tab focus in WPF - wpf

I am New in WPF
I have one form on which I put one datagrid as well as there is one "Add New" button to add new item in grid
so is there any way to know about tab lost focus when click on Add new button and again get focus on tab control ? Lostfocus event of tab control does not work.
Thanks

You can make use of the TabControl.SelectionChanged Event
void TabControl_TabSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl)
{
// do something
}
}

Related

WPF popup listbox use keyboard for navigation

Can anyone suggest the solution how can I make navigation using down and up keys in listbox which come on popup.
Solutions like set selected items on keyup and keydown event are not working for me.
Should I make something more special then just set selected item in this case?
ListBox already implements selection navigation using keyboard when it is focused.
All you have to do is give it focus when you want, for example in the window that contains it:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
listbox.SelectedIndex = 0;
listbox.Focus();
}
}
Because listbox.Focus(); will only give it focus but won't yet change the selection item (which will make the user hit's the "Down" button twice in order to do so) set the ListBox's SelectedIndex first.
Hope this helps

Dynamically create ContextMenu in Silverlight 5

I have just started to use Silverlight this few days.
Currently I have generated some panel dynamically in a canvas. Each panel will have a shared MouseLeftButtonUp event which will display a ContextMenu.
The problem now is when I click on a first panel, the ContextMenu is able to show but when I click on a second panel, the first ContextMenu shown up instead of the second. The second panel will show its ContextMenu only when I click another time on the second panel.
Below is my code to generate the panel and the context menu:
void generatePanel()
{
StackPanel panel = new StackPanel();
panel.MouseLeftButtonUp += panel_MouseLeftButtonUp;
canvas.Children.Add(panel)
}
void panel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
ContextMenu menu = new ContextMenu();
menu.IsOpen = true;
}
Hopefully my problem is clear enough. Is there any way to solve the problem? Or if there are any event that are fired when the menu is hidden, so that I can make sure the previous context menu is disposed or cleared?
Thanks.

POPUP on Button right click + WPF

I have a ListBox that is bound to a list of items. The ListBoxItem is bound to a datatemplate of type Button.
On the click of button, I do some action (another window is shown). So i have bound to the Command of the button.
Now my requirement is that i show a POPUP (with some buttons in popup) to the right click of the button.
How would i be able to do this in MVVM ?
Girija
You can simply catch MouseUp event from ListBox.ItemTemplate and set Popup.IsOpen there:
private void SomeTemplateElement_MouseUp(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Right)
puMyPopup.IsOpen = true;
}
Popup is a view element, so I don`t think there`s a reason to open it through viewmodel commands.

How to disable hide/unhide in WPF Ribbon Control

I am using the latest Ribbon Control from MS. When one double click the Tab Header, the whole Ribbon will hide the content and show only the header text. I want to disable this hide/unhide feature. So even if user double click Tab Header, the Ribbon remain as it is.
I think it should be the OnMouseDoubleClick Event in RibbonTab but have no clue how to override it. Am I suppose to give it a x:Name to each and every RibbonTab then write an empty method ribbonTab1_OnMouseDoubleClick to each Ribbon Tab?
Use the PreviewMouseDoubleClick event in the Ribbon:
PreviewMouseDoubleClick="ribbon_PreviewMouseDoubleClick"
and then in the handler, mark the event as handled:
private void ribbon_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}

ToolStrip Buttons flashes

I have a windows form with a toolstip in it with several buttons.
When the mouse is over a button of the toolstip then the toolstrip button starts to flash... looks like it gets and loses focus every second.
That results for the click to do nothing if the user click at the time that the button has no focus therefore the user has to click the button again and again util he gets the timing correct.
Does anyone knows anything about this?
I rally need some answers as soon as possible...
Thank you very much
I have found the reason...
The toolstrips in windows forms have by default the tooltips set to Auto and if the tooltip opens on the taskbar then the toolstrip loses focus.
The solution to this is to either disable the tooltips or to set it to manual and show the tooltip at another place.
Here is the code for showing the tool tip above the item manually:
private readonly ToolTip currentToolTip = new ToolTip();
private void ToolStripItem_MouseEnter(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Show(item.ToolTipText, item.Owner, item.Bounds.X, -20);
}
private void ToolStripItem_MouseLeave(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Hide(item.Owner);
}
You have to add the event handlers to all your ToolStripItems and set the ToolStrips' ShowItemToolTips to false.

Resources