BarButtonItem & RibbonBarManager Click Event in DevExpress - winforms

I have a problem with the DevExpress namely: Button in RibonControl.
I wrote the event "click" for the way the button in RibonControl as follows:
private void barButtonClick_ItemClick(object sender, ItemClickEventArgs e)
{
BarButtonItem item = (BarButtonItem)(sender);
//..... if else ...
}
and I get an error message while running:
Additional information: Unable to cast object of type 'DevExpress.XtraBars.Ribbon.RibbonBarManager' to type 'DevExpress.XtraBars.BarButtonItem'.
I think, this error due to the fact that "click" of the button in DevExpress similar in WPF (routed events), correct? and how to solve?

Here is how the correct code-snippet looks for your task:
void barButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
BarButtonItem bItem = e.Item as BarButtonItem;
// ... do something
}
Documentation: the ItemClickEventArgs.Item property.
The fact, that sender parameter within this event handler is BarManager rather than the BarButtonItem, can be explained via DevExpress XtraBars Suite architecture.
The main idea is that the BarItem is a non-visual element that can't be "clicked". The BarItemLink element is a link to this item that represent item on screen. The BarManager instance manage all the interactions with links and route all the events to the corresponding item's and BarManager's event handlers. That's why the BarManager/RibbonBarManager instance appears as sender parameter within the event handler.
See Items and Links for more details.

Related

Is there a possibility to programmatically trigger item action in ListView, like when you dobuleclick on it?

I was searching all over the place but I couldn't find an answer. I need to fire up an ListView item action, so it would rise the ItemActivate event. For now, it's only possible using ENTER key or double click... I would like to know if I could programmatically do that, something like :
listView.Items[int].Activate();
This doesn't work of course, because that function Activate() is not implemented there. For example, I couldn't find how to trigger buttons programmatically, but there it was, in the context menu which appears while you type:
buttonX.PerformClick();
...and it would trigger the button_click event. I wonder if there's something similar in the ListView control for triggering items inside of it ? I want it to raise this event programmatically and not by mouse doubleclick or Enter key on the keyboard...
private void myListView_ItemActivate(object sender, EventArgs e)
{
MessageBox.Show("hello");
}
According to the documentation, the EventHandler in the ListView should be public. So you can raise the event with:
myListView.ItemActivate(myListView, EventArgs.Empty);
This way everyone who subscribed to this event will get notified.
Another way, of course, is to directly call your method:
myListView_ItemActivate(this, EventArgs.Empty);
But this doesn't really classify as "raising the event", because you actually don't raise an event. You just call a method.

Silverlight DataGrid MouseLeftButtonDown event not raised when clicking on rows

Note: I have found a solution to my problem so I am posting this for reference purposes, although I would be happy to be educated with a better solution.
I'm trying to provide double click functionality on a Silverlight DataGrid by hooking into the UIElement.MouseLeftButtonDown but when I subscribe to the DataGrid.MouseLeftButtonDown using XAML or the DataGrid.MouseLeftButtonDown += syntax, my event handler is not called when I click on the rows within the DataGrid. If I click on the Header, the event is raised.
If I subscribe to the same event at the parent UserControl level, the event handler is called successfully as you would expect based on Silverlight RoutedEvents but then I have to detect whether the click occurred on the DataGrid or somewhere else.
If I subscribe to the event using this UIElement.AddHandler syntax, as shown below, then it works as expected based on the handledEventsToo: true parameter.
dataGrid.AddHandler(UIElement.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(dataGrid_MouseLeftButtonDown)
, handledEventsToo: true);
It seems that the DataGrid implementation is marking these events as handled, prevent event bubbling, by default in one of the child UIElements, which is not what I expected initially. With more thought I can see that the click behaviour drives all sorts of things (select item, edit field etc.) so perhaps the implementation makes sense.
I had the same problem and I used MouseLeftButtonUp which fires the event but the clickcount value is always 1.
Here is the fix for that:
private const int MOUSE_SENSITIVITY = 300;
private DateTime _previousClick;
private void exceptionsDataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
DataGrid dg = (sender as DataGrid);
DateTime current=DateTime.Now;
LoggerService.Exception exception = (LoggerService.Exception)dg.SelectedItem;
if (_previousClick != null)
{
TimeSpan clickSpan = current - _previousClick;
if (clickSpan.TotalMilliseconds < MOUSE_SENSITIVITY)
{
MessageBox.Show("You double clicked!");
}
}
_previousClick = current;
}

How can I prevent a WP7 context menu from closing after an item has been selected?

I have hooked up a context menu item in a listbox item such that tapping it will change its state. I need either the menu to stay open after the item has been selected, or to programmatically reopen the menu right after it closes.
My menu looks like so:
Some Command 1
Some Command 2
Some Command 3
Inverted
And the user can tap the Inverted command and then tap one of the other commands to cause them to function in Inverted Mode, and the menu through data binding appears like so:
Some Command 1
Some Command 2
Some Command 3
Inverted ✔
Not being able to figure out how to keep the menu open after tap, I've tried the less ideal reopen menu approach like so:
private void onCommandInvert(object sender, RoutedEventArgs e)
{
CommandState.Instance.Inverted = !CommandState.Instance.Inverted;
// Open it again.
MenuItem menuItem = (MenuItem)sender;
ContextMenu menu = (ContextMenu)menuItem.Parent;
menu.IsOpen = true;
}
But doing so throws the following exception on the menu.IsOpen = true statement:
A first chance exception of type 'System.InvalidOperationException' occurred in
System.Windows.dll
An unhandled exception of type 'System.InvalidOperationException' occurred in
System.Windows.dll
Additional information: Element is already the child of another element.
I have also tried the following with the Closed event, with the same exception occurring:
private void onContextMenuClosed(object sender, RoutedEventArgs e)
{
ContextMenu menu = (ContextMenu)sender;
menu.IsOpen = true;
}
Any ideas? Thanks!
I got it! Thanks to willmel's comment, I digged through the source code for the MenuItem and was able to override OnClick() to do exactly what I needed (the ideal solution no doubt). I couldn't access Click however, so I needed to introduce a StayClick event property as well.
Enjoy!
using Microsoft.Phone.Controls;
using System.Windows;
namespace MyNamespace
{
public class MenuItemEx : MenuItem
{
public bool StayOpenWhenClicked
{
get;
set;
}
public event RoutedEventHandler StayClick;
protected override void OnClick()
{
if (StayOpenWhenClicked)
{
if (StayClick != null)
{
StayClick.Invoke(this, new RoutedEventArgs());
}
}
else
{
base.OnClick();
}
}
}
}
and in the page's xaml, instead of toolkit:MenuItem you use my:MenuItemEx
<my:MenuItemEx
Header="Inverted"
StayClick="onCommandInvert"
StayOpenWhenClicked="True"
/>
If you want to persist the menu after the user has selected an item then I believe the Context menu control is not what you should be using.
Better you should create your own user control to mimic the behaviour and have it place appropriately on the screen where it makes sense (to the side or above/below)
Alternatively if these are options to be enacted on selected items consider using the application bar icons / menu items and write the event code to read the currently selected value of the listbox item.
Hope this helps.

How to register to/listen to richtextbox command's?

I'm creating a simple editor within our application using the WPF RichTextBox. Above it I've added the reguslar buttons like Bold, Italic, etc. These buttons use the RichTextBox's commands to set these properties, but next to these buttons, the commands also get send with CTRL+B, CTRL+I, etc. I want these buttons to represent the current state of the RichTextBox at the cursor. I already found out how to get this state and it works when I update this state on the SelectionChanged event. This event ofcourse isn't fired when Bold is toggled so there is no direct feedback.
I would like to know if there is a way to listen to the commands being called, without affecting its original behaviour or some other ideas to solve my problems.
I tried listening to the command the following way:
CommandBinding boldBinding = new CommandBinding(EditingCommands.ToggleBold, CommandExecuted);
_richTextBox.CommandBindings.Add(boldBinding);
and
private void CommandExecuted(object sender, ExecutedRoutedEventArgs e) {
UpdateProperties();
e.Handled = false;
}
This did update the properties, but the RichTextBox didn't seem to receive the command anymore.
I also tried to make my own commands on the control containing the RichTextBox, but when CTRL+B is pressed when the RichTextBox has focus, the original RichTextBox commands are called instead of the new one.
Many thanks in advance!
Liewe
In order to listen to the commands being called, you can use the events raised by CommandManager: Executed or PreviewExecuted.
If you change your XAML to:
<RichTextBox x:Name="_richTextBox" ...
CommandManager:PreviewExecuted="OnRichTextBoxCommand" ... />
you get the OnRichTextBoxCommand method called right before the command is executed. Unfortunately, using the Executed attached event does not work.
This method is called for each event, so you have to filter them:
private void OnRichTextBoxCommand(object sender, ExecutedRoutedEventArgs e) {
if (e.Command == EditingCommands.ToggleBold) {
UpdateProperties();
}
}
It may be even a bit more complex, as the current selection may not have changed when this method is called, so you have to post yourself a message, e.g. like this:
Dispatcher.BeginInvoke(new Action(UpdateProperties));
(if you reference already System.Core, you have the Action type, otherwise define a delegate taking no parameter and returning void, and use in instead.)

How to capture a mouse click on an Item in a ListBox in WPF?

I want to get notified when an item in a ListBox gets clicked by the mouse, whether it is already selected or not.
I searched and found this: (http://kevin-berridge.blogspot.com/2008/06/wpf-listboxitem-double-click.html see the comments)
private void AddDoubleClickEventStyle(ListBox listBox, MouseButtonEventHandler mouseButtonEventHandler)
{
if (listBox.ItemContainerStyle == null)
listBox.ItemContainerStyle = new Style(typeof(ListBoxItem));
listBox.ItemContainerStyle.Setters.Add(new EventSetter()
{
Event = MouseDoubleClickEvent,
Handler = mouseButtonEventHandler
});
}
//Usage:
AddDoubleClickEventStyle(listView1, new MouseButtonEventHandler(listView1_MouseDoubleClick));
This works, but it does it for a DoubleClick. I can't get it working for a single click though. I tried MouseLeftButtonDownEvent - as there doesn't seem to be a MouseClick event, but it's not being called.
A bit more general side question: How can I see what events do exist and which handlers correspond to them and when they actually do something? For example, what tells me that for a MouseDoubleClickEvent I need a MouseButtonEventHandler? Maybe for a MouseLeftButtonDownEvent I need some other handler and that's why it's not working?
I also tried subclassing ListBoxItem and override OnMouseLeftButtonDown - but it doesn't get called either.
Marc
I believe that your MouseLeftButtonDown handler is not called because the ListBox uses this event internally to fire its SelectionChanged event (with the thought being that in the vast majority of cases, SelectionChanged is all you need). That said, you have a couple of options.
First, you could subscribe to the PreviewLeftButtonDown event instead. Most routed events have a routing strategy of Bubbling, which means that the control that generated the event gets it first, and if not handled, the event works its way up the visual tree giving each control a chance at handling the event. The Preview events, on the other hand, are Tunneling. This means that they start at the root of the visual tree (generally Window), and work their way down to the control that generated the event. Since your code would get the chance to handle the event prior to the ListBoxItem, this will get fired (and not be handled) so your event handler will be called. You can implement this option by replacing MouseDoubleClickEvent in your sample with PreviewMouseLeftButtonDown.
The other option is to register a class handler that will be notified whenever a ListBoxItem fires the MouseLeftButtonDown event. That is done like this:
EventManager.RegisterClassHandler(typeof(ListBoxItem),
ListBoxItem.MouseLeftButtonDownEvent,
new RoutedEventHandler(this.MouseLeftButtonDownClassHandler));
private void OnMouseLeftButtonDown(object sender, RoutedEventArgs e)
{
}
Class Handlers are called before any other event handlers, but they're called for all controls of the specified type in your entire application. So if you have two ListBoxes, then whenever any ListBoxItem is clicked in either of them, this event handler will be called.
As for your second question, the best way to know what type of event handler you need for a given event, and to see the list of events available to a given control, is to use the MSDN documentation. For example, the list of all events handled by ListBoxItem is at http://msdn.microsoft.com/en-us/library/system.windows.controls.listboxitem_events.aspx. If you click on the link for an event, it includes the type of the event handler for that event.
There is also another way - to handle PreviewMouseDown event and check if it was triggered by the list item:
In XAML:
<ListBox PreviewMouseDown="PlaceholdersListBox_OnPreviewMouseDown"/>
In codebehind:
private void PlaceholdersListBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var item = ItemsControl.ContainerFromElement(sender as ListBox, e.OriginalSource as DependencyObject) as ListBoxItem;
if (item != null)
{
// ListBox item clicked - do some cool things here
}
}
Was inspired by this answer, but it uses listbox by name, I propose to use sender argument to avoid unnecessary dependencies.
I think the first option in Andy's answer, of using PreviewMouseLeftButtonDown, is the way to go about this. In XAML it would look like this:
<ListBox Name="testListBox">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter
Event="PreviewMouseLeftButtonDown"
Handler="ListBox_MouseLeftButtonDown" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
There is another way to get MouseDown event in ListBox. You can add event handler for events that are marked as handled by using handledEventsToo signature of AddHandler method:
myListBox.AddHandler(UIElement.MouseDownEvent,
new MouseButtonEventHandler(ListBox_MouseDown), true);
Third parameter above is handledEventsToo which ensures that this handler will be invoked no matter if it is already marked as Handled (which ListBoxItem does in ListBox).
See Marking Routed Events as Handled, and Class Handling for explanation.
See How to Attach to MouseDown Event on ListBox for example.
You can use Event="MouseLeftButtonUp"
Unlike "PreviewLeftButtonDown" it will get the ListBoxItem handled too.
You can use the SelectionChangedEventArgs argument of the SelectionChanged event to find what item is add or removed through AddedItems and RemovedItems, usually only have the latest clicked on, or if not, then look at the last item which is the count-1.

Resources