I am searching for Volume Slider that looks and behave just like VLC's slider.
I found the following post about how to style the slider: Volume Slider CustomControl
but I also want the same behavior...
What is the difference between the behaviors:
When you click on the slider [at the WPF] and move the mouse on the slider area (while mouse button still being hold) it should move the slider also to the position of the mouse on the slider.
I couldn't find how to do it.. maybe I should use something different than Slider?
Thanks for the help!
There is a property on the slider called IsMoveToPointEnabled that sets the slider to the correct value but only when you click it doesn't update as you drag.
To update as you drag you have to update the value yourself when the mouse is moved, the method Track.ValueFromPoint gives you the correct value, the track is part of the sliders template.
Example
public class DraggableSlider : Slider
{
public DraggableSlider()
{
this.IsMoveToPointEnabled = true;
}
private Track track;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
track = Template.FindName("PART_Track", this) as Track;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if(e.LeftButton == MouseButtonState.Pressed && track != null)
{
Value = track.ValueFromPoint(e.GetPosition(track));
}
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
base.OnPreviewMouseDown(e);
((UIElement)e.OriginalSource).CaptureMouse();
}
protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
{
base.OnPreviewMouseUp(e);
((UIElement)e.OriginalSource).ReleaseMouseCapture();
}
}
The OnPreviewMouseUp/Down overrides capture the mouse, I tried VLC and that doesn't capture the mouse so you could remove them if you wanted. Capturing the mouse allows the value to change even if the mouse leaves the control similar to how scrollbars work.
Related
I have WPF desktop application with a Button. When I run it on normal PC and go with mouse cursor over the button, it becomes blue (default Windows theme). When I move cursor out, button is gray again. Pretty normal behavior.
But when I run it on Windows 8 tablet, following is happening: I touch the Button, it becomes blue. Then I move up my finger, but button stays blue. There is no MouseLeave event. I see blue button until I click somewhere else on the screen.
Is there any workaround how to prevent this? I know I can remove the whole hover effect, but I don't want to do that unless there is another way.
check whether following (http://blakenui.codeplex.com/) will help you to handle the issue
WPF: Is there a possibility to "route" ordinary mouse events to touch events in Windows 7
I was able to fix that by using following behavior which uses visual states:
public class TouchDeviceMouseOverUIElementFixBehavior : Behavior<UIElement>
{
protected override void OnAttached()
{
AssociatedObject.StylusUp += AssociatedObject_StylusUp;
}
protected override void OnDetaching()
{
AssociatedObject.StylusUp -= AssociatedObject_StylusUp;
}
private void AssociatedObject_StylusUp(object sender, StylusEventArgs e)
{
var control = sender as FrameworkElement;
if (control != null)
{
if (!VisualStateManager.GoToElementState(control, "Normal", true))
{
VisualStateManager.GoToState(control, "Normal", true);
}
}
}
}
You can do this by removing default mouse hover option in WPF. It worked perfectly fine for me.
Here is the source i found the answer
I have am making a map editor for a game which has a user control that has an image. Inside that control I attached the MouseWheel event to it, but I've noticed two issues that I hope to have a better understanding of why it behaves the way it does and how to properly implement it.
For one the event only seems to fire when the mouse is hovering over it instead of when the control is in focus. If possible I would like to switch that and be able to fire the event no matter where the mouse is as long as that control is in focus and the second issue is that checking the delta when the number is positive works fine, but when I get a number back when it's negative I get a value of 0xfffffffd or something in that range. How would I go about differentiating the difference between a positive balue and a negative value if I always get something positive?
Thanks in advance for the help.
If you want to fire MouseWheel event for focused element try:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.MouseWheel += OnMouseWheel;
}
IInputElement focusedElement;
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
{
if (focusedElement is TextBox)
{
var tbx = focusedElement as TextBox;
//do something
}
}
protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
focusedElement = e.NewFocus;
}
}
I have a WPF window .I want when my mouse cursor is outside the Control area of window and I am clicking on it I want my window to disappear is there any mechanism of achieving it thorough WPF??
Take a look at the Mouse.Capture method. This lets you get mouse events even if the mouse is outside your control.
Be sure to release the mouse once you're done though calling Capture with null.
To release mouse capture, call Capture passing null as the element to capture.
In the constructor place this:
public MyControl()
{
//Other stuff like initialize component
Mouse.Capture(this);
MouseLeftButtonDown += OnMouseLeftButtonDown;
}
Then implement that method:
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if(!this.IsMouseOver)
{
Close(); //your closing implementation here
Mouse.Capture(null);
}
}
I have 2 controls on a form. One numericUpDown (from the Silverlight Toolkit) and a simple Rectangle.
On the MouseLeftButtonDown of the Rectangle I popup a MessageBox with the numericUpDown value.
If I use the arrows to change the value of the numericUpDown, everyting is fine. But if I edit the value manually (with the keyboard) and immediately click on the Rectangle it shows the previous value of the numericUpDown. If I click a sencond time on the rectangle it will show the new value.
The numericUpDown.ValueChanged event is raised after the Rectangle.MouseLeftButtonDown event.
Is that a Silverlight bug? Anybody knows a workaround for that?
(btw I cannot change my Rectangle controls or events)
As workaround I propose you to create your own control like:
public class MyNumericUpDown : NumericUpDown
{
private TextBox _textBox;
public void Sync()
{
ApplyValue(_textBox.Text);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_textBox = (TextBox)GetTemplateChild("Text");
}
}
Now you can use method Sync to syncronize display text with control Value property. You can call method from XAML declaratively or in code behind. In your case:
private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
myNumericUpDown.Sync();
MessageBox.Show(myNumericUpDown.Value.ToString());
}
I creating a custonmized box class (inherits from ComboBox). I don't want the text box to react to right mouse clicks. I can get rid of the context menu by setting this to null in ApplyTemplate, but right mouse clicks move the cursor. I tried hooking up PreviewMouseRightButtonDown in ApplyTemplate and setting Handled to True, but the event still gets through which is strange as it seems to work for the left click.
The cursor actually moves when the mouse button is released, so you want mark the MouseRightButtonUp event as handled. You could override OnMouseRightButtonUp:
protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
{
base.OnMouseRightButtonUp(e);
e.Handled = true;
}
Or you could attach a class handler to the MouseRightButtonUp event to mark it as handled:
static MyComboBox()
{
EventManager.RegisterClassHandler(
typeof(MyComboBox),
MouseRightButtonUpEvent,
new MouseButtonEventHandler(MyComboBox_MouseRightButtonUp));
}
private static void MyComboBox_MouseRightButtonUp(
object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
That will also prevent the context menu from being created without you having to set it to null explicitly.