2013-10-30 50 views
0

我应该如何同一个数据源绑定到列表框的ItemsSource和列表框项模板e.gWPF绑定与父母

<ListBox ItemsSource="{Binding TrackPluginView }" BorderBrush="Transparent" Height="180" ScrollViewer.VerticalScrollBarVisibility="Auto" 
    Grid.IsSharedSizeScope="True" 
    HorizontalContentAlignment="Stretch"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical"> 
          <ContentControl Width="auto" Height="auto" Content="{ Binding TrackPluginView}" /> 
          <Button Command="{Binding}" Content="Delete" Width="150" HorizontalAlignment="Right" VerticalAlignment="Center" ></Button> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
+2

你可以在绑定的项目使用的RelativeSource模式= FindAncestor – ZSH

回答

0
<ListBox ItemsSource="{Binding TrackPluginView }" BorderBrush="Transparent" Height="180" ScrollViewer.VerticalScrollBarVisibility="Auto" 
Grid.IsSharedSizeScope="True" 
HorizontalContentAlignment="Stretch"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Vertical"> 
        <ContentControl Width="auto" Height="auto" Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=DataContext.TrackPluginView}" /> 
        <Button Command="{Binding}" Content="Delete" Width="150" HorizontalAlignment="Right" VerticalAlignment="Center" ></Button> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox>