【发布时间】:2014-04-02 13:25:14
【问题描述】:
我有一个这个模型(它不是实际代码,为了清楚起见,我省略了 INotifyPropertyChanged 实现)。
public class Project
{
public ObservableCollection<Component> Components { get; set; }
}
public class Component
{
public ObservableCollection<Item> Items { get; set; }
}
public class Item
{
public LookupItem LookupItem { get; set; }
}
这是 ViewModel
public class ViewModel
{
public Project Project { get; set; }
public Components { get { return Project.Components; } }
public ObservableCollection<LookupItem> LookupItems { get; set; }
}
在视图中,我有两个列表视图,一个显示组件,另一个绑定到第一个显示所选组件的项目。后一个列表视图应该有一个用于任何项目的组合框以更改查找项,但我无法绑定它。
这是xaml
<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>
我无法填充组合框。 Wpf 给了我错误
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')
【问题讨论】:
标签: c# wpf xaml listview binding