2009-11-15 62 views
7

我有一個ListBox。它有內部的ScrollViewer,所以我可以用鼠標滾輪滾動ListBox內容。它工作正常,直到我設置包含另一個ListBox的項目模板(實際上,我有4個嵌套的ListBoxes =))。問題在於內部ListBox的ScrollViewer竊取了輪迴事件。有沒有簡單的方法來防止這種行爲?如何禁用ListBox中的ScrollViewer?


我曾與ItemContainerStyle ListBox的是這樣的:

<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="BorderBrush" Value="Black"/> 
    ... 
</Style> 
<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}" /> 

如何設置樣式在這樣的資源ItemContainer的項目邊界?正如我所知ContentPresenter是ItemsControl的項目容器。但它沒有邊界,所以我無法設計它。

回答

40

您可以通過改變其控制模板的東西更簡單刪除從ListBoxScrollViewer

<ListBox> 
    <ListBox.Template> 
     <ControlTemplate> 
      <ItemsPresenter /> 
     </ControlTemplate> 
    </ListBox.Template> 
    ... 
</ListBox> 

然而,我懷疑嵌套列表框的值。請記住,每個ListBox是一個選擇器,並具有哪個項目被「選中」的概念。在所選項目內選定項目內是否有選定項目是否真的有意義?

我建議將「內部」ListBoxes更改爲簡單ItemsControls,以便嵌套列表不能包含選定項目。這會使用戶體驗更簡單。您可能仍然需要以相同的方式重新模板內部的ItemsControls以移除滾動條,但至少用戶不會對「選擇」哪個項目感到困惑。

+0

_Does真的有意義有一個選定的項目中選定的項目,所選項目內_是。如何從列表中的列表中選擇一個項目? – wotanii 2016-09-23 07:49:26

+0

這也從列表框中刪除了很多其他的東西,例如, drop-events – wotanii 2016-09-23 07:55:56

0

對不起,醒了這麼舊的帖子。實際上,您可以使用ScrollViewer的附加屬性來禁用ScrollViewer。

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
     ScrollViewer.VerticalScrollBarVisibility="Disabled" ... 
</ListBox> 
+4

這似乎仍然偷走了輪迴事件 – wotanii 2016-09-23 07:43:49

+0

它不適用於我:( – Sam 2017-11-25 07:52:06

+0

它也不適用於我。 – Eftekhari 2018-01-10 14:51:58

-1

您可以使用它!沒有車輪被盜。

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
    ScrollViewer.VerticalScrollBarVisibility="Disabled" ... 
</ListBox> 
1

您可以禁用通過XAML捕獲滾動事件偷滾動事件:

<ListBox PreviewMouseWheel="ScrollViewer_PreviewMouseWheel"> 

,並重新將其發佈在後面的代碼:

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e) 
    { 
     if (sender is ListBox && !e.Handled) 
     { 
      e.Handled = true; 
      var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta); 
      eventArg.RoutedEvent = UIElement.MouseWheelEvent; 
      eventArg.Source = sender; 
      var parent = ((Control)sender).Parent as UIElement; 
      parent.RaiseEvent(eventArg); 
     } 
    } 

的解決方案正是爲列表框,它幫助我使用ListView。

我在這裏找到這個解決方案:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/3a3bb6b0-e088-494d-8ef2-60814415fd89/swallowing-mouse-scroll?forum=wpf

相關問題