【问题标题】:Operating a slider inside a ListBox by touch通过触摸操作 ListBox 内的滑块
【发布时间】:2014-06-30 15:35:17
【问题描述】:

我有一个ItemTemplate 和一个ListBoxItem,其中包含一个Slider。 UI 应该在触摸屏上操作。但是,滑块不起作用 - 无法拖动拇指。

问题可以这样证明:

<StackPanel>
    <Slider Height="40" Width="300" Margin="20,10,20,10"/>
    <ListBox>
        <Slider Height="40" Width="300" Margin="20,10,20,10"/>
    </ListBox>
</StackPanel>

ListBox 内的滑块不能通过触摸拖动(尽管它适用于鼠标或键盘输入)。 ListBox 之外的另一个滑块工作正常(鼠标、键盘、触摸)。

需要做什么才能“启用”ListBox 内的滑块以进行触摸输入?

【问题讨论】:

  • 您需要编辑 Lisbox 模板。此处的基本模板:msdn.microsoft.com/ru-ru/library/cc278062(v=vs.95).aspx
  • 在此模板中究竟需要做什么才能启用滑块?
  • @JeffRSon 可能是ListBox 的默认选择行为干扰了触摸事件。如果您不需要选择,请使用 ItemsControl 而不是 ListBox。
  • @JeffRSon 让这个完全清楚,键盘和鼠标输入工作吗?只是触摸事件不起作用吗?
  • @JeffRSon 我无法在这里测试触摸输入,但您可能想使用Snoop 查看事件序列。它会告诉您引发了哪些事件,以及哪些 UI 元素正在处理它们。

标签: wpf listbox touch


【解决方案1】:

Slider \ ScrollViewer in a touch interface not working properly 接受的答案也会在这里有所帮助。

这可能是由于ListBoxItemsControl 内部包含ScrollViewer。显然这个ScrollViewer 对拖动ListBox 本身很重要(所以我无法摆脱它)。但是,ScrollViewer 可能会被其他帖子的 CustomThumb 类欺骗。

替换样式中的Thumb 很简单:

<Window
    ...
    xmlns:l="clr-namespace:WpfApplication1"
    ...>
<Window.Resources>
    ...
    <Style x:Key="SliderStyle1" TargetType="{x:Type Slider}">
        ...
        <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Slider}">
            ...
            <Track x:Name="PART_Track" Grid.Row="1">
                <Track.Thumb>
                    <l:CustomThumb x:Name="Thumb" .../>
                </Track.Thumb>
            </Track>
            </ControlTemplate>
        </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

您可以通过编辑 Expression Blend 创建的样式来做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 2017-11-30
    • 1970-01-01
    相关资源
    最近更新 更多