【问题标题】:Scrolling of a listbox inside another listbox在另一个列表框内滚动列表框
【发布时间】:2013-02-03 08:36:28
【问题描述】:

我有一个列表框(具有正常的垂直方向),其中的每个元素都是一个具有 水平 方向的列表框。 我想在 internal 列表框内有一个 ScrollBar。所以,我的问题是如何根据外部当前的实际宽度设置内部列表框的宽度。

我当前的代码是:

<Window.Resources>
    <HierarchicalDataTemplate x:Key="ItemTemplateSchedule">
        <ListBox>
            <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBoxItem>My-Very-Long-Item-Nimber-1___</ListBoxItem>
            <ListBoxItem>My-Very-Long-Item-Nimber-2___</ListBoxItem>
            <ListBoxItem>My-Very-Long-Item-Nimber-3___</ListBoxItem>
            <ListBoxItem>My-Very-Long-Item-Nimber-4___</ListBoxItem>
            <ListBoxItem>My-Very-Long-Item-Nimber-5___</ListBoxItem>
        </ListBox>
    </HierarchicalDataTemplate>
</Window.Resources>

<Grid>
    <ListBox ItemTemplate="{StaticResource ItemTemplateSchedule}" >
        >
    </ListBox>
</Grid>

当前截图: .

UPD1

好的,我的问题的答案是设置内部列表框的宽度,感谢 @sa_ddam213

Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=ActualWidth}"

现在我想在外部列表框的每一行中添加一些新控件:

<HierarchicalDataTemplate x:Key="ItemTemplateSchedule">
    <StackPanel Orientation="Horizontal" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=ActualWidth}">
        <TextBlock Text="This is Text in a TextBlock"/>
        <ListBox >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBoxItem>My-Very-Long-Item-Number-1___</ListBoxItem>
            <ListBoxItem>My-Very-Long-Item-Number-2___</ListBoxItem>
            <ListBoxItem>My-Very-Long-Item-Number-3___</ListBoxItem>
            <ListBoxItem>My-Very-Long-Item-Number-4___</ListBoxItem>
            <ListBoxItem>My-Very-Long-Item-Number-5___</ListBoxItem>
        </ListBox>
    </StackPanel>
</HierarchicalDataTemplate>

现在不行了!有可能解决这个问题吗?当前截图:

【问题讨论】:

    标签: wpf layout listbox


    【解决方案1】:

    您可以使用FindAncestor 绑定到父ListBox ActualWidth

    例子:

    <Window.Resources>
        <HierarchicalDataTemplate x:Key="ItemTemplateSchedule">
            <ListBox Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=ActualWidth}" >
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBoxItem>My-Very-Long-Item-Nimber-1___</ListBoxItem>
                <ListBoxItem>My-Very-Long-Item-Nimber-2___</ListBoxItem>
                <ListBoxItem>My-Very-Long-Item-Nimber-3___</ListBoxItem>
                <ListBoxItem>My-Very-Long-Item-Nimber-4___</ListBoxItem>
                <ListBoxItem>My-Very-Long-Item-Nimber-5___</ListBoxItem>
            </ListBox>
        </HierarchicalDataTemplate>
    </Window.Resources>
    

    结果:

    【讨论】:

    • 谢谢!它工作正常。但是,如果我想在外部布局的每一行中插入一些新控件,它就不起作用。请参阅我的帖子中的 UPD1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多