Edit: Rewritting question
我在WPF用户控件中使用http://dlhsoft.com/Home.aspx中的项目管理库 .
我在我的页面上显示他们的LoadChartResourceListView控件,并使用datatemplate在列表视图中显示自定义列:
<my:LoadChartResourceListView TaskManagerSource="{Binding ElementName=ganttChartTaskListView,Path=TaskManager}"
TimelinePageStart="{Binding TimelinePageStart, ElementName=ganttChartTaskListView, Mode=TwoWay}"
TimelinePageFinish="{Binding TimelinePageFinish, ElementName=ganttChartTaskListView, Mode=TwoWay}"
DisplayedTime="{Binding DisplayedTime, ElementName=ganttChartTaskListView, Mode=TwoWay}"
Margin="6" Name="loadChartResourceListView">
<my:LoadChartResourceListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource ListViewColumnHeaderContainerStyle}">
<!-- Set CellTemplate (or CellTemplateSelector) to specify column templates. -->
<GridViewColumn Header="Group" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox Width="85" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type inf:MEFUserControl}}, Path=DataContext.ResourceGroups}"
DisplayMemberPath="GroupName"
SelectedValuePath="GroupID" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Resource">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="myTB" Text="{Binding Content}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
包含此LoadChartResourceListView的整个用户控件(inf:MEFUserControl)将datacontext设置为我的viewmodel类(TaskData)的实例 . 在TaskData类中是 ObservableCollection<ResourceGroup> ResourceGroups {get;set;}
. 每个ResourceGroup都有 int GroupID {get;set;}
和 string GroupName{get;set;}
.
此外,在TaskData类中是 ObservableCollection<Resource> Resources {get;set;}
...每个资源都有一个int GroupID{get;set;}
,字符串 Content {get;set;}
和 ResourceGroup ResGroup{get;set;}
上面的代码可以很好地显示组合框和文本框...我不能,为了我的生活,弄清楚为什么我有很多东西,包括 SelectedValue="{Binding GroupID}"
每当我尝试设置SelectedValue时,我在VS中收到此错误弹出:"A first chance exception of type 'System.Reflection.AmbiguousMatchException' occurred in mscorlib.dll"这是输出窗口中的错误(其大量)http://pastebin.com/AGJwn00C
从阅读开始,我读到这是由于父对象具有同名“GroupID”的属性 . 我已经在Resource类中将GroupID重命名为ResGroupID,认为它与ResourceGroup类冲突,但是我收到了同样的错误 .
当我设置这个ItemsSource时,组合框的DataContext是否设置为UserControl或TaskData实例?
Update:
当我使用TextBox而不是组合框时,我也收到错误:
<TextBox Text="{Binding GroupID}"/>
2 回答
写吧
SelectedValue =“”
解决了它 . 看完之后:http://dlhsoft.com/KnowledgeBase/Task-Appearance-Bar-Templating-Custom-Data-Binding.htm
我必须为自定义属性执行Item.PropertyName .