0

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

而且现在不行!有可能解决这个问题吗?当前截图: 更新1

4

1 回答 1

1

您可以使用 aFindAncestor绑定到父级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>

结果:

在此处输入图像描述

于 2013-02-18T20:09:38.713 回答