在另一个列表框中滚动列表框

时间:2013-02-18 20:01:25

标签: wpf layout listbox

我有一个列表框(垂直方向正常),每个元素都是一个水平方向的列表框。 我希望在内部列表框中包含一个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>

目前的截图: Listbox inside listbox

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>

现在它不起作用!有可能解决这个问题吗?当前截图: upd1

1 个答案:

答案 0 :(得分: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>

结果:

enter image description here