Skip to main content
Home Forums Silverlight Programming Programming with .NET - General click on position to move slider
3 replies. Latest Post by Mog Liang - MSFT on March 6, 2009.
(0)
skm.soft...
Member
196 points
216 Posts
03-01-2009 12:51 AM |
Hi
I am using slider control in a project in silverlight 2,To change the position of movable slider bar on horizontal linewe have to drag the movable slider bar from mouse to that position in horizontlalline of slider control,But I need functionallity that when I click on a certain position of horizontal bar, the slider(movable bar) will automaticallymove to that position.
fullsail...
Contributor
3699 points
829 Posts
03-01-2009 1:07 AM |
Handle the MouseLeftButtonDown on the Slider. Use MouseEventArgs e.GetPosition(theSliderControl).X to get an offset of where they've clicked. Then take the offset and set the position.
sunstate9
66 points
25 Posts
03-02-2009 6:23 PM |
You have to get mouse position for the moveing scrollbar so
in mouseleftbuttondown(object e,mousetEventargs e)
{
point p=new point();
p=e.getposition(this);
//Now you should assigen p.x to slider as you wish...
}
If it may help U ,
Mark as answer
Mog Lian...
All-Star
15884 points
1,541 Posts
03-06-2009 5:54 AM |
Slider don't implement the MouseLeftButtonDown event, so we can't capture mouse action by handling this event.
I write a slider inherent from Slider, exposed custom Click event.
CustomSlider Code:
public class CustomSlider: Slider { public CustomSlider() { DefaultStyleKey = typeof(Slider); } public override void OnApplyTemplate() { base.OnApplyTemplate(); RepeatButton rb; rb = base.GetTemplateChild("HorizontalTrackLargeChangeIncreaseRepeatButton") as RepeatButton; rb.MouseMove += new MouseEventHandler(rb_MouseMove); rb.Click += new RoutedEventHandler(rb_Click); rb = base.GetTemplateChild("HorizontalTrackLargeChangeDecreaseRepeatButton") as RepeatButton; rb.MouseMove += new MouseEventHandler(rb_MouseMove); rb.Click += new RoutedEventHandler(rb_Click); rb = base.GetTemplateChild("VerticalTrackLargeChangeIncreaseRepeatButton") as RepeatButton; rb.Click += new RoutedEventHandler(rb_Click); rb.MouseMove += new MouseEventHandler(rb_MouseMove); rb = base.GetTemplateChild("VerticalTrackLargeChangeDecreaseRepeatButton") as RepeatButton; rb.Click += new RoutedEventHandler(rb_Click); rb.MouseMove += new MouseEventHandler(rb_MouseMove); //rb. } Point ps; void rb_MouseMove(object sender, MouseEventArgs e) { ps=e.GetPosition(this); } public event EventHandler MyClickEvent; void rb_Click(object sender, RoutedEventArgs e) { if(MyClickEvent!=null) MyClickEvent(this,new PositionEventArgs(ps)); } } public class PositionEventArgs : EventArgs { public Point Position { set; get; } public PositionEventArgs() { } public PositionEventArgs(Point p) { Position = p; } }
Then, we use this slider to implement "click and positioning" job.
XAML:
<
XAML code behind:
private void CustomSlider_MyClickEvent_1(object sender, PositionEventArgs e) { double progress = sl1.Maximum * e.Position.X / sl1.ActualWidth; sl1.Value = progress; }