Button simulating keyboard "Shift" key pressed - wpf

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;
}

Related

Click outside component to unfocus

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.

How to detect a click on the selected row of a RadGridView control

My WPF application uses the Telerik RadGridView control on a few of its screens. I have a requirement that says that whenever the user clicks on a row in the RadGridView, I'm supposed to switch to another screen and display detail information about that row. I hooked up a handler to the SelectionChanged event and this works, except that nothing happens if the user clicks on the selected row a second time. This makes sense, as the selected row isn't being changed.
How can I detect a second click on the same row and have the second screen displayed?
Tony
You could just attach a handler to the MouseUp event on the GridView. Check if there are any selected cells and respond from there. This will fire even if there is already a selection.
The MouseDown event will fire on the mouse click, but before the gridview updates the selction, mouse up should fire when the selection has already been adjusted
You can also attach a handler to each individual cell in code-behind as follows
(this.GridView as RadGridView).AddHandler(
GridViewCell.MouseUpEvent,
new EventHandler<Telerik.Windows.RadRoutedEventArgs>(this.OnMouseUp));
I think you may try to achieve this through the MouseLeftButtonDown event or PreviewMouseLeftButtonDown event.

GridViewColumnHeader.Click triggered by clicking ListView's scrollbar shaft & scroll arrows

I am using attached behaviours to add the ability to sort a ListView by clicking on a column's header. The behaviour adds the following handler to handle the user clicking on a GridViewColumnHeader:
listView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
The handler looks something like this:
static void ColumnHeader_Click(object sender, RoutedEventArgs e)
{
var listView = sender as ListView;
var header = e.OriginalSource as GridViewColumnHeader;
var gridView = ((GridView)(listView.View));
...
}
I just noticed that if the ListView has a scroll bar, and I click on the scroll bar's 'shaft' or scroll arrows (but not thumb!):
(source: microsoft.com)
then the GridViewColumnHeader.ClickEvent is triggered, and my code fails because 'header' is now null. Obviously this isn't an expected behaviour, and now I have to make sure that the OriginalSource is a GridViewColumnHeader.
Why does this happen?
The problem is, GridViewColumnHeader.ClickEvent actually is ButtonBase.ClickEvent, which means that the handler you set will be triggered by a user click on any button (or derived: checkbox, datepicker, scrollbar button, etc.) in the ListView, and not only on a column header (which is derived from button).
See http://social.msdn.microsoft.com/Forums/en/wpf/thread/3595d153-4faa-4501-9c6f-2f074658e760
The only solution I've found is to check that header != null before doing anything else, to exit the handler when the button that triggered it (e.OriginalSource) was not a column header.
Hope this helps...
(PS: also check if header.Column != null, to avoid an error when the user clicks on the "last extra header", the empty header on the right of all the "regular" headers...)

Control.Leave Triggered by MouseDown, not MouseUp

I have a C# .NET WinForm. In the form, I allow a user to add an item to a ListView by double-clicking in the ListView. This adds a TextBox control to the ListView and places the key cursor in the TextBox so a user can type.
I detect that a user is done with an item in a couple of ways (e.g. pressing Enter, Esc, Tab...), but also when they Leave (TextBox.Leave) the TextBox.
The problem is this set of steps:
User triggers TextBox.Leave by mousing down outside of the TextBox.
I add the new item to the ListView.
I select the the new item in the ListView.
Mouse up occurs and the new item that I just selected, loses focus and is unselected.
What I would like is for TextBox.Leave to be triggered by MouseUp, not MouseDown. How can I accomplish this?
Edit: Cody suggests using the ListView.LabelEdit property. Here are my results trying that:
listView_DoubleClick(...) {
listView.LabelEdit = true;
if(double clicked on existing listViewItem) {
listViewItem.BeginEdit(); //this works as expected
} else {
var newItem = listView.Items.Add("");
newItem.BeginEdit(); //this doesn't work, see below
}
}
The call to newItem.BeginEdit() only works when the user double clicks where the new item will show up. If they double click on any other blank area in the listview the new item is added, but it does not enter edit mode. What's going on here?
Pressing the mouse down on another control is causing that other control to request the focus and so the focus moving causes the TextBox.Leave event to occur. Preventing ever other possible control from requesting the focus is not a very viable option. But luckly you only need to prevent the ListView from using the MouseDown to shift focus. So you need to override the WndProc of your ListView and when the MouseDown windows message occurs and you are currently showing a TextBox you eat the message. In order words you do not allow the base class to process it.

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