Click outside component to unfocus - wpf

By default, I need to focus on another component (a button or a textBox) to unfocus from the current one.
I want to just click outside.
So for example if I click on a textBox and write something, than click outside the TextBox, I shouldn't be able to type because the component is unfocused.
(I hope my explanation is clear, if not, please say so in the comments)

You can achieve this functionality by attaching to PreviewMouseDown event of a Window. Also in order to Lose Focus you need another focusable control (e.g. a text box named _dummyControl) to move focus to.
public MainWindow()
{
this.PreviewMouseDown += PreviewMouseDownEventHandler;
}
private void PreviewMouseDownEventHandler(object sender, MouseButtonEventArgs e)
{
//check conditions here...
_dummyControl.Focus();
}
more info:
PreviewMouseDown event occurs just before a MouseDown or click event occurs. By attaching to this event your code can tell when user clicks on the window and at that time you should move focus to a hidden control to simulate unfocus functionality.

Related

Prevent drop-down menu to close

I have a big problem. I want to prevent an opened drop-down menu to close when hovering another menu item. How can I achieve this? Thanks.
Regards
Marius
You should have a look at the event that's called when you are hovering another item, probably LostFocusor LostMouseCapture, look at the function called aith this event (OnLostFocus or OnLostMouseCapture) and override this method like this :
public override void OnLostFocus()
{
base.OnLostFocus();
Popup popup = Template.FindName("PART_Popup", this) as Popup;
popup.IsOpen = true;
}
This way, the event won't close the drop down menu. (The example above is for a combobox)
I'm not sure about the right event to override. It depends on your problem. You can find the list of event here if these are not the right ones. But I think that's the way you should do it.

Button simulating keyboard "Shift" key pressed

I Have a WPF window that have a RadGridView and a Button,
My requirement is, when you click on the button and
select a row in the grid, it should work similar to
pressing on the shift key and clicking on a gridview row.
(To select multiple rows).
So, i need to press the shift key programatically in button
click event.
Can this be done?
Appreciate if anyone can provide a solution
Do not fire a KeyPress event manually. Rather, on this page, Telerik indicates that to allow multiple selection, you must set the SelectionMode to Extended:
this.radGridView.SelectionMode = System.Windows.Controls.SelectionMode.Extended;
So, put this in your button click (I guess you'll want it to toggle between Extended and Single. Multiple's behaviour is awkward, but that's just me) and you're good to go.
Edit:
The toggle button will have something like this in its click event handler:
private void toggleButton1_Click(object sender, RoutedEventArgs e)
{
this.radGridView.SelectionMode = toggleButton1.IsChecked ? SelectionMode.Extended : SelectionMode.Single;
}

How to close a dialogue box from some custom link inside the dialogue box

I have custom control which I am rendering inside dialogue box.
this custom control has a link lable lnkLable. I want to close the opened window when I click on lnkLable.
right now I am finding the parent of my conrol which will be dialogue control in the end and then calling the dispose method of that, which I don't feel very good technique to do this.
Use the Close() method on the form to close it.
private void button1_Click(object sender, EventArgs e)
{
Control btn = sender as Control;
Form frm = btn.Parent as Form;
frm.Close();
}
If it's a modal dialog you can also close it by calling the Hide() method because modal dialogs are automatically destroyed the modal pump exits, and the pump will exit when the dialog is hidden.
Rather try using Control.FindForm Method
You have to remember that the control might not be directly on the form, but inside another container, like a panel, in which case the parent of your control will not be the form.
Once yuo have the instance of the form, rather use Form.Close Method

Listbox MousedoubleClick in WPF

I have code written on my Listbox mouse double click. I have a submit button too in my form . On mouse double click i want a mouse double click event to be fired first and then the button click
Is it possible?
Please reply
Thanks
Sharath
Easiest (and best practise) is simply to put the code for what to do on Button click into a seperate method (say OnOkClicked), then call this method from the Button Click event handler, as well as at the end of the ListBox DoubleClick event handler.
I nice pattern to implement is to implement an ICommand (like Josh Smith's RelayCommand - google it) on your modelview class, bind the button's Command property to it, and then in the list box's DoubleClick handler, invoke the Executed method of the command. This strucutre simplifies maintaining the logic of:
If nothing is selected in the list box, disable the OK button (Command.CanExecute would return false)
If something is selected in the list box, enable the OK button
If the list box is double clicked, select an item and invoke the OK command.
Hope this helps.
you can make your own control and implement ICommandSource which will give you the same properties

How do I disable the exit button on a Silverlight 3 Child Window?

I mean the small exit/cancel button marked with an X in the top right hand corner. I want to implement a Logon dialog box that accepts a username/password so obviously I don't want the user to be able to dismiss the modal pop up. If it is not possible to remove or disable the button then is there some way I can intercept the closing event and stop it closing?
You can use the HasCloseButton property of the ChildWindow to hide the close button.
Please let me know if this helps.
Ezequiel Jadib
The code below prevents a ChildWindow from ever closing, effectively disabling the X button. Modify to suit your business logic.
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
e.Cancel = true;
}
Select child window and Press F4. It will show the property window. Then goto HasCloseButton property and uncheck the checkbox.
Enjoy
HasCloseButton="False" ..
This property used to hide the 'X' button In ChildWindow

Resources