C# Wpf主详细信息列表视图绑定

C# Wpf主详细信息列表视图绑定,c#,wpf,xaml,listview,binding,C#,Wpf,Xaml,Listview,Binding,我对此模型有一个简单的描述(它不是实际的代码,为了清晰起见,我省略了INotifyPropertyChanged实现) 尝试将您的ViewModel添加为数据上下文,例如,对于窗口,如下所示: <Window.DataContext> <local:ViewModel /> </Window.DataContext> 在DataTemplate中写下: <DataTemplate> <Grid HorizontalAlign

我对此模型有一个简单的描述(它不是实际的代码,为了清晰起见,我省略了INotifyPropertyChanged实现)


尝试将您的
ViewModel
添加为数据上下文,例如,对于
窗口
,如下所示:

<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
在DataTemplate中写下:

<DataTemplate>
    <Grid HorizontalAlignment="Stretch">
        <ComboBox ItemsSource="{Binding Path=DataContext.LookupItems, 
                                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},
                                        Mode=TwoWay}" ... />
    </Grid>
</DataTemplate>

   <ListView x:Name="list" ItemsSource="{Binding Components}">
       ......
   </LIstView>
  <ListView  ItemsSource="{Binding Items}" DataContext="{Binding SelectedItem, ElementName=list}">
....
<GridViewColumn Width="140">
    <GridViewColumnHeader Tag="Publisher" Content="Item" />
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch">
                <ComboBox SelectedItem="{Binding Path=Item}" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ViewModel}}, Path=LookupItems, Mode=TwoWay }" DisplayMemberPath="Name" />
            </Grid>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>
  System.Windows.Data Error: 4 : Cannot find source for binding with 
  reference 'RelativeSource FindAncestor, AncestorType='Projectname.ViewModels.ViewModel', AncestorLevel='1''. BindingExpression:Path=LookupItems; DataItem=null; target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
var viewModel = new ViewModel();
this.DataContext = viewModel;
<DataTemplate>
    <Grid HorizontalAlignment="Stretch">
        <ComboBox ItemsSource="{Binding Path=DataContext.LookupItems, 
                                        RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}},
                                        Mode=TwoWay}" ... />
    </Grid>
</DataTemplate>