POPUP on Button right click + WPF - 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.

Related

WPF UserControl GotFocus

I have a UserControl that contains several ComboBoxes and Buttons.
In the window that hosts the UserControl I need to update some properties when the UserControl gets the focus.
My problem is, that every time, when the focus inside the user control changes, that get a GotFocus event in the hosting windows.
Is there some kind of best practice to make sure, that I only get one GotFocused event in the hosting window? I mean, if I step through controls inside the UserControl the focus is always inside the UserControl, so I don't want a GotFocused event.
This is the solution I came up to:
First of all this post was the key for my solution: WPF UserControl detect LostFocus ignoring children .
And Refer to active Window in WPF? .
Using the functions in these posts I registered the LostFocus event in my UserControl.
private void UserControl_LostFocus(object sender, RoutedEventArgs e)
{
var focused_element = FocusManager.GetFocusedElement(Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive));
var parent = (focused_element as FrameworkElement).TryFindParent<KeywordSelector>();
if (parent != this) userControlHasFocus=false;
}
And then ...
private void UserControl_GotFocus(object sender, RoutedEventArgs e)
{
if (userControlHasFocus == true) e.Handled = true;
else userControlHasFocus = true;
}
This way I keep track of the focus. userControlHasFocus is false be default. When the GotFocus() happens for the first time it's false and the GotFocus event is not stopped form bubbling up. But userControlHasFocus gets set to true, because now the focus is inside the UserControl.
Whenever the focus moves to another control, LostFocus checks if the new controls parent is the UserControl. If not, it resets the userControlHasFocus to false.

How to click controls inside Popup with StaysOpen as False

I have a Popup with a ListView inside. I'd like to set StaysOpen as False so that it properly closes when I click away from the popup. However, this means that all mouse events are intercepted. As such, I don't get any mouse events inside my ListView.
Here's my current setup. I've removed all styling to make it easier to see what's going on.
<Popup Name="puSearchResults" StaysOpen="False"
AllowsTransparency="True"
LostFocus="puSearchResults_LostFocus"
LostKeyboardFocus="puSearchResults_LostKeyboardFocus"
LostMouseCapture="puSearchResults_LostMouseCapture" >
<ListView Name="lvSearchResults"
MouseLeftButtonDown="lbSearchResults_MouseLeftButtonDown"
SelectionChanged="lvSearchResults_SelectionChanged"/>
</Popup>
In this case, MouseLeftButtonDown only works if I set StaysOpen to True, but then the Popup doesn't disappear if I click away.
Ideas?
I would add mouse events to the ListView or to the objects in your ListView's data templete.
Then you could do this:
private void lvSearchResults_MouseEnter(object sender, MouseEventArgs e)
{
puSearchResults.StaysOpen = true;
}
private void lvSearchResults_MouseLeave(object sender, MouseEventArgs e)
{
puSearchResults.StaysOpen = false;
}
EDIT
Similar question that should help: Close Wpf Popup On click of item of its own control template

how to get Tabcontrol's Tab focus in 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
}
}

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.

Click event in UserControl- WPF

I have a UserControl in my WPF application.
I want to call a click event and do some things when the user clicked the UserControl.
The problem is- the UserControl doesn't have a click event.
I searched on the web and found that you can use the MouseLeftButtonUp event.
I tried it- but it doesn't respond to my clicks.
You didn't write what you are trying to do but if you need a click event maybe you are writing some kind of button (the Button class is actually "something you can click" with the visual representation in a control template you can replace)
If you need a button with complex content inside - put your user control inside a button
If you need a button that doesn't look like a button write a custom control template for button
If you need a button with extra functionality subclass button, add the extra data/behavior in code and put the display XAML in a style.
I think for your needs PreviewMouseLeftButtonUp(Down) event is more suitable. Then you need to handle ClickCount for counting amount of clicks and then raise your own event, on which other controls will know, that your control is clicked. There are much more methods on handling click event. You should look at this msdn article and this
UPDATE to handle both Click and DoubleClick
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_myCustomUserControl.MouseLeftButtonUp += new MouseButtonEventHandler(_myCustomUserControl_MouseLeftButtonUp);
_myCustomUserControl.MouseDoubleClick += new MouseButtonEventHandler(_myCustomUserControl_MouseDoubleClick);
}
bool _doubleClicked;
void _myCustomUserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
_textBlock.Text = "Mouse left button clicked twice";
_doubleClicked = true;
e.Handled = true;
}
void _myCustomUserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_doubleClicked)
{
_doubleClicked = false;
return;
}
_textBlock.Text = "Mouse left button clicked once";
e.Handled = true;
}
}
To test this example name your control as _myCustomUserControl and add a TextBlock named _textBlock to your MainWindow.xaml
Why not just use MouseDown?
Put the event in the User Control and simply do this:
private void MyControl_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
MessageBox.Show("Clicked!");
}
}

Resources