WPF与父级绑定

时间:2013-10-30 06:48:50

标签: wpf

如何将相同的数据源绑定到ListBox ItemsSource及其列表框项模板,例如

<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>

1 个答案:

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