Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

wpf - How to disable ScrollViewer in ListBox?

I have a ListBox. It has internal ScrollViewer, so I can scroll ListBox content with mouse wheel. It works fine until I set item template that contains another ListBox (in fact, I have 4 nested ListBoxes =)). Problem is that internal ListBox's ScrollViewer steals wheeling event. Is there any easy way to prevent this behavior?


I had ListBox with ItemContainerStyle like this:

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

How can I set style for ItemContainer's item border in resources like this? As I know ContentPresenter is item container of ItemsControl. But it hasn't Border, so I can't style it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can remove the ScrollViewer from a ListBox by changing its control template to something much simpler:

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

However, I question the value of nesting ListBoxes. Remember that each ListBox is a Selector and has a concept of which item is "selected". Does it really make sense to have a selected item inside a selected item, inside a selected item?

I would suggest changing the "inner" ListBoxes to simple ItemsControls so that the nested lists can't have selected items. That would make for a much simpler user experience. You may still need to retemplate the inner ItemsControls in the same way to remove the scrollbars, but at least the user won't get confused about which item is "selected".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...