如何在DataTemplate中使用嵌套ListBox滚动ListBox

时间:2018-10-11 20:34:07

标签: .net wpf xaml

为了简化描述问题,我举了一个小例子。 这是xaml:

<Window x:Class="ScrollViewerTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ScrollViewerTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
    <local:ViewModel></local:ViewModel>
</Window.DataContext>
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled"
              CanContentScroll="True">
    <ListBox ItemsSource="{Binding Path=Items}">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="local:ClassOuter">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Path=Text}" Margin="5"></TextBlock>
                    <ListBox ItemsSource="{Binding Path=Items}" Margin="20 0">
                        <ListBox.ItemTemplate>
                            <DataTemplate DataType="local:ClassInner">
                                <TextBlock Text="{Binding Path=Text}"></TextBlock>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</ScrollViewer>

无论ListBox如何结束,如何用鼠标滚轮滚动ScrollViewer?

enter image description here

0 个答案:

没有答案