2013-02-18 147 views
0

我有一個列表框(具有正常的垂直方向),其中的每個元素都是一個列表框,其中水平方向爲。 我想在內部列表框中有一個滾動條。所以,我的問題是如何根據實際當前的外部寬度來設置內部列表框的寬度。在另一個列表框內滾動列表框

我當前的代碼是:

<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

可以使用FindAncestor綁定到父ListBoxActualWidth

例子:

<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

+0

謝謝!它工作正常。但是如果我想在外部佈局的每一行插入一些新的控件,它不起作用。在我的帖子中查看UPD1。 – 2013-02-19 03:32:08