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>

そして、それは今は機能しません!この問題を解決することは可能ですか?現在のスクリーンショット: upd1

4

1 に答える 1

1

FindAncestora を使用して親にバインドできます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 に答える