私は、これは可能な限り最高のUXではありません知っているが、それでも、私はこれを達成したいですしたがってそのValue
)は通常通り2倍遅く移動します。ファインpositionningのスライダーサム
これはこれまで私が行ってきたことです。
public partial class CustomSlider : UserControl
{
public CustomSlider()
{
InitializeComponent();
CMiXSlider.ApplyTemplate();
Thumb thumb0 = (CMiXSlider.Template.FindName("PART_Track", CMiXSlider) as Track).Thumb;
thumb0.MouseEnter += new MouseEventHandler(thumb_MouseEnter);
}
private void thumb_MouseEnter(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && e.MouseDevice.Captured == null)
{
MouseButtonEventArgs args = new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Left);
args.RoutedEvent = MouseLeftButtonDownEvent;
(sender as Thumb).RaiseEvent(args);
}
}
Point pStart;
Point pCurrent;
Point pCenter;
bool isDragging;
private void CMiXSlider_DragStarted(object sender, DragStartedEventArgs e)
{
isDragging = true;
if (Keyboard.IsKeyDown(Key.LeftShift))
{
pStart = Mouse.GetPosition(CMiXSlider);
}
}
private void CMiXSlider_DragDelta(object sender, DragDeltaEventArgs e)
{
pCurrent = Mouse.GetPosition(CMiXSlider);
if (Keyboard.IsKeyDown(Key.LeftShift) == true)
{
pCenter.X = Math.Abs(pCurrent.X - pStart.X);
pCenter.Y = Math.Abs(pCurrent.Y - pStart.Y);
Value = (1.0/CMiXSlider.ActualWidth) * pCenter.X/2 + (1.0/CMiXSlider.ActualWidth)*pStart.X;
if (Value >= 1.0)
{
Value = 1.0;
}
else if (Value <= 0.0)
{
Value = 0.0;
}
}
}
private void CMiXSlider_DragCompleted(object sender, DragCompletedEventArgs e)
{
isDragging = false;
}
private void CMiXSlider_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftShift) && isDragging == true)
{
pStart = Mouse.GetPosition(CMiXSlider);
}
}
とXAML:しかし
<UserControl x:Class="CMiX.CustomSlider"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CMiX"
mc:Ignorable="d"
d:DesignHeight="116.3" d:DesignWidth="567.2">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/CMiX_UserControl;component/RessourceDictionnaries/Brushes/GenericBrushes.xaml"/>
<ResourceDictionary Source="/CMiX_UserControl;component/RessourceDictionnaries/Styles/BaseSliderStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Slider x:Name="CMiXSlider" Style="{StaticResource BaseSliderStyle}" Margin="1"
Thumb.DragDelta="CMiXSlider_DragDelta"
Thumb.DragStarted="CMiXSlider_DragStarted"
Orientation="{Binding Orientation, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type local:CustomSlider}}}"
Value="{Binding Value, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type local:CustomSlider}}}"
IsMoveToPointEnabled="False" Minimum="0.0" Maximum="1.0" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"/>
いくつかの問題。今度は最初にShift
を押してからドラッグしなければなりません。ドラッグしてからシフトすると、親指が後ろに飛びます。
私はどこかのロジックが不足しています。
アドバイスはありますか?
はあなた、あなたが何ができるか
をコントロールするための更新'CMiXSlider.ActualWidth'と同じですので、effectiveChang eは無限大またはNaNです。 – lecloneur
@lecloneur 'Thumb'として' CMiXSlider'を使用しましたので、あなたの場合はおそらく代わりに 'thumb0.ActualWidth'を使う必要があります。それ以上の問題がある場合は、完全なコード(XAMLを含む)を投稿してください。 –
がXAMLを追加しました – lecloneur