关于wp7中的滑块控件的一些问题 [英] some questions about the slider control in wp7
问题描述
1:Slider控件具有两个视觉元素:轨道"和拇指".如何设置轨道的高度和宽度?我只能在其属性(矩形)中设置滑块的高度和宽度,如果高度稍小(例如:height = 50),则轨迹消失.
2:由于wp7不支持拖动事件(DragEnter,DragLeave,DragOver),因此当我沿着轨道拖动拇指以更改滑块的值时如何处理该事件?
1:A Slider control has two visual elements: the "track" and the "thumb". How can I set the track''s height and width? I can only set the slider''s height and width in its property(which is a rectangle),and if the height is a little smaller(for example:height=50),the track disappears.
2:Since the drag event(DragEnter,DragLeave,DragOver)is not support by wp7,how can I handle the event when I drag the thumb along the track to change the value of the slider?
推荐答案
您可以执行以下操作,因为拇指没有拖动事件,因此可以从后面的代码中调用mouse_enter事件,并在拇指滑过时使用ICommand来获取滑块的值,代码看起来像这样
you can do something like this, since there is no drag event for the thumb you can call the mouse_enter event from code behind and use an ICommand to fetch the value of the slider when the thumb is slided over, the code looks something like this
private System.Windows.Controls.Primitives.Thumb _thumb;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (_thumb != null)
{
_thumb.MouseEnter -= new MouseEventHandler(thumb_MouseEnter);
}
if (_thumb == null)
{
System.Windows.Controls.Primitives.Track track = this.Template.FindName(
"PART_Track", this) as System.Windows.Controls.Primitives.Track;
if (track != null)
_thumb = track.Thumb;
}
if (_thumb != null)
{
_thumb.MouseEnter += thumb_MouseEnter;
}
}
void thumb_MouseEnter(object sender, MouseEventArgs e)
{
if (!this.IsMoveToPointEnabled)
return;
if (e.LeftButton == MouseButtonState.Pressed)
{
// the left button is pressed on mouse enter
// but the mouse isn't captured (when MouseEnter fires), so the thumb
// must have been moved under the mouse in response
// to a click on the track.
// Generate a MouseLeftButtonDown event so that
// the user can just drag the thumb without having to click again.
var args = new MouseButtonEventArgs(
e.MouseDevice, e.Timestamp, MouseButton.Left);
args.RoutedEvent = MouseLeftButtonDownEvent;
(sender as System.Windows.Controls.Primitives.Thumb).RaiseEvent(args);
}
}
public static readonly RoutedEvent CommandChangedEvent = EventManager.RegisterRoutedEvent(
"CommandChangedEvent",
RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<icommand>),
typeof(XSlider));
public event RoutedPropertyChangedEventHandler<icommand> CommandChanged
{
add { AddHandler(CommandChangedEvent, value); }
remove { RemoveHandler(CommandChangedEvent, value); }
}
protected virtual void OnCommandChanged(ICommand oldValue, ICommand newValue)
{
RoutedPropertyChangedEventArgs<icommand> args = new RoutedPropertyChangedEventArgs<icommand>(oldValue, newValue);
args.RoutedEvent = XSlider.CommandChangedEvent;
RaiseEvent(args);
if (cmd_CanExecuteChangedHandler == null)
cmd_CanExecuteChangedHandler = cmd_CanExecuteChanged;
if (oldValue != null)
oldValue.CanExecuteChanged -= cmd_CanExecuteChangedHandler;
if (newValue != null)
newValue.CanExecuteChanged += cmd_CanExecuteChangedHandler;
else
cmd_CanExecuteChangedHandler = null;
UpdateCanExecute();
}
// hold a reference to it, it might me stored as a weak reference and never be called otherwise...
EventHandler cmd_CanExecuteChangedHandler;
void cmd_CanExecuteChanged(object sender, EventArgs e)
{
UpdateCanExecute();
}
void UpdateCanExecute()
{
if (IsCommandExecuting)
return;
ICommand cmd = Command;
if (cmd == null)
{
IsEnabled = true;
}
else
{
try
{
IsCommandExecuting = true;
var ca = new CommandCanExecuteParameter(CommandParameter);
bool enabled = CommandUtil.CanExecute(this, CommandParameter);
IsEnabled = enabled;
CommandUtil.CanExecute(this, ca);
if (enabled && ca.CurrentValue != null)
{
if (ca.CurrentValue is float)
{
Value = (float)ca.CurrentValue;
}
else if (ca.CurrentValue is double)
{
Value = (double)ca.CurrentValue;
}
if (ca.CurrentValue is RangedValue)
{
var rangedValue = (RangedValue)ca.CurrentValue;
Value = rangedValue.Value;
if (rangedValue.Minimum.HasValue)
Minimum = rangedValue.Minimum.Value;
if (rangedValue.Maximum.HasValue)
Maximum = rangedValue.Maximum.Value;
}
}
}
finally { IsCommandExecuting = false; }
}
}
private bool IsCommandExecuting { get; set; }
注意:请根据您的类中使用的变量名称进行更改
希望对您有帮助!
Note: Pls make changes according to the variable names used in your class
Hope this helps!!
这篇关于关于wp7中的滑块控件的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!