WPF Mouse Over - c#

What is the best way to link a method when the mouse over event is fired?
An example.
when a mouse hovers over a button. A method fires such as count.
So as long as the mouse is over the button, the count will keep on incrementing.
How will i connect a mouse over event to a method? Using Xaml?!
ANY IDEAS?!

One way to do this is via an attached property, especially if you want to reuse this capability.
Expression Blend's Behavior<T> class makes this simple. You'll need the the Blend SDK, but then you can use behaviors directly. Here's a blog post showing an example.
This allows you to assign an attached behavior in XAML. Many MVVM frameworks have an implementation that uses this approach, such as the LifetimeEvents in Cinch.

Related

WPF MVVM C# Input Bindings With TextBox key presses

I am currently stuck on an issue that I have been unable to find an answer for. It involves 'overriding' built-in TextBox keyboard functions using MVVM. Here's my issue:
I have a TextBox that the users enters text into during runtime. I'm looking to implement keyboard shortcuts, or input bindings with this TextBox. I started out by using what seems to be the standard ICommand interface based approach, which I implemented simply as:
<TextBox.InputBindings>
<KeyBinding Key="Up" Command="{Binding testCommand}" Modifiers="Ctrl" />
</TextBox.InputBindings>
The idea is that I execute the command associated with the ICommand property as set in the ViewModel. However, the TextBox class already contains a baked in function for CTRL+UP which, as you may know, moves the caret to the beginning of the TextBox entry field. This function executes in addition to my Command, resulting in my desired changes, but also the caret moving.
Before MVVM, I would simply use the PreviewKeyDown event in Code-Behind, detecting the desired key combinations and using
e.Handled = true;
to stop the TextBox from firing its built in Command. This isn't an optimal solution, however, as it probably violates MVVM principles. It would also be difficult to implement in tandem with InputBindings declared in XAML.
During my research, I found an article that describes using a class provided by Blend that allows for executing a Command based off an event, potentially allowing me to listen for the PreviewKeyDown event, which fires before the built-in functions, but this would mean that I would not be declaring my key bindings in XAML, as the syntax does not seem to support keypress specific conditions, or InputBindings. I would have to do all key combination processing logic in the ViewModel, which seems like something the View should be doing.
Further research has led me to an interesting new area which involves intercepting and replacing certain 'ApplicationCommands' with a Command that always returns false on CanExecute, effectively removing the function. I was able to locate a case where someone wanted to replace the default CTRL+Z functionality. An abridged version of the full question shows the basic idea:
textBox.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo,
UndoCommand, CanUndoCommand));
Research of this ApplicationCommands class led me to a few other classes, one of which relates more directly to my issue: ComponentCommands. MSDN describes that class as containing (among other things)
ComponentCommands.MoveToHome
which would fit my Ctrl+Up command's built-in function. Using the syntax in the SO question I linked previously, I could theoretically prevent that command from executing, which is a step in the right direction. However, I'm hoping to make the key combinations that trigger my Command in my ViewModel customizable via ViewModel properties that the InputBindings would be bound to in XAML. If I implemented this approach, it would mean that I could not know ahead of time which built in functions might conflict with the user's chosen key bindings.
This was naturally resolved for me before I switched to MVVM by handling the arguments in the event handler in Code-Behind, as mentioned previously. Any key combinations I wrote into the PreviewKeyDown event handler that I had a special action for set the e.Handled to true, effectively preventing any possible built-in command that would follow once the method finished from executing after the event handler finished.
Is there some way I could implement this functionality, using InputBindings declared in XAML? What are my options?
Some ideas:
Perhaps I could expose the event handler for PreviewKeyDown on my TextBox, and inside the method I iterate through all of the TextBox's InputBindings, looking for provided key combinations that match the provided key press, and if found, I execute the associated command and set e.handled to true? I'm not familiar with manipulating InputBindings from Code-Behind, so I'm not sure how practical this would be, or if it would place too much load on the application (imagine a user holding down a key). It also seems a bit strange in terms of organization to set it up this way. I could see a situation happening where the input bindings fire twice.
I could also make a list of all the built-in commands that I'd want to always have around, even if the user set a key combination that conflicted, and disable the rest. That seems like a bad idea, as it would be quite laborious, and also could mess with something I couldn't anticipate.
Perhaps there's a way that I could calculate which built-in commands would conflict with the current input bindings and disable those, to be calculated each time the user sets the bindings?
I'm still learning MVVM and WPF, so I suspect that I may be missing something obvious or simply looking at it the wrong way. Any help would be greatly appreciated.
Edit:
It has been suggested that my question is a duplicate of this one. I have reviewed the answer and determined that the approach used is definitely a step in the right direction. I could use the attached object to implement the PreviewKeyDown handling without losing functionality of InputBindings in XAML. However, I do have some concerns.
It seems that the resolution was to move the InputBindings to the UserControl, and implement an attached object that would handle PreviewKeyDown. I have multiple TextBoxes in my UserControl which will need their own set of InputBindings, some of which would conflict with one another should they be moved to the UserControl level.
I can only conclude that the reason it was moved away from the TextBox was that the attached object could not be applied directly to the TextBox for some reason. If I was to attempt this, would I have to create some encompassing element that had the attached object that would hold each TextBox? Why can't the TextBox use the attached object itself?

WPF: Get MouseEnter Event When IsEnabled=false

I want to handle MouseEnter event for a Custom Control when control is disabled.
Is there a way to handle it?
The documentation for UIElement.IsEnabled documents the behaviour you see:
Elements that are not enabled do not participate in hit testing or focus and therefore will not be sources of input events.
The logical conclusion, to me, is that if you do want to handle mouse events, you don't disable the control. Instead, use some other method of achieving what you want. For example, if it's an input control, it may be good enough to make it read-only instead of disabled. Your question doesn't really explain why you want this, so I cannot guess what the right method for you will be.
perhaps you could surround your custom control with a ContentControl (which must always be enabled) and handle MouseEnter event on the ContentControl.

WP7 Complex Layout

I want to make a complex layout, and would like advice on the best way to go about this.
Here is the behaviour I'm looking for: Layout of some text blocks and some images, such that a tap anywhere on the layout will go to another related page to that item. I want a long list of this item in a scroll viewer.
First, I tried to make a grid, add the items in it, then add this grid in the outer grid, the main one for pressing, I made an event handler for mouse click, but a problem appeared; that when I try to scroll "on the item to view the all list" the event handler fired!, I tried the mouseButton up and down, and the same happen
Second, I'm not sure if it's an efficient way to make it.
I want a good way to design this complex layout, and of course it will be a programmatic way, and a way to recognize the item I press on.
It sounds like you're trying to create your own ListBox control. Using this will give you the scrolling functionality for free, and you can use the ListBox's SelectionChanged event to determine when an item has been clicked.
The best way to design the layout will depend on how complex it actually is. If it merely contains an image and some text, you should have no problem hand-coding that in XAML. If there's a lot more to it than that, I'd recommend looking into Microsoft's Blend tool. Whichever route you choose to produce the XAML, you will place the code in the ListBox's ItemTemplate, which determines how each item in the ListBox is displayed.
A good walkthrough of something similar to what you're doing can be found at http://weblogs.asp.net/psheriff/archive/2010/10/27/windows-phone-list-box-with-images.aspx

Why does nothing in WPF have a click event?

This seems very strange to me.
I know through some adventurous inheritance you can convert most UIElements to a button, but it is a cumbersome way of implementing the most basic of of computer events
The ButtonBase class certainly has a Click event. So has derived classes.
I think the most basic events are MouseDown and MouseUp which are available to every UIElement
use RoutedEvents event if you really need to use.
Other wise use dependency property to register a Click event against your control and then do what ever you want to do.
You can add RoutedEvents to your controls.
Here is an implementation example

WPF usercontrol and external events

I'm a rookie in c# and I'm working on a player with an specialized card. I've work on a usercontrol with the typical buttons stop play, pause and record. The problem is that I don't know how to create the events such that from the principal code (Window) I could manage and call the correspondent function, since from the usercontrol file I can't acces any of the items (of course defined in window) I need to make the things work, as they are not accesible.
Thanks in advance
Best regards,
Oscar
Your UserControl needs to expose appropriate properties, methods and events, so that your Window can interact with it. For example, your UserControl might declare a Paused event, so that the Window could respond when the user pauses the control; or it might declare a Play method, so that the Window can start the control playing. When you instantiate your UserControl in the Window's XAML file, give it a name, e.g.
<local:MyControl x:Name="myControl" />
You can then refer to it from code-behind, e.g.:
myControl.Paused += MyControl_Paused;
myControl.Play();
To create the required API, create public properties, methods and events on the UserControl in the code-behind class. You say you are a "rookie in C#" so I don't know how much guidance you need, but unless you need to get into WPF data binding or event routing, you can use normal C# / .NET properties, methods and events for this. Declaring properties, methods and events in C# is widely covered in MSDN and the literature. If you specifically need help working with WPF data binding or event routing, leave a comment outlining your specific difficulty and I'll update the answer.

Categories