WPF DataTemplate ComboBox 绑定问题
编辑:重写问题
我使用 http://dlhsoft.com/Home 中的项目管理库.aspx 在我的 WPF 用户控件中。
我在页面上显示他们的 LoadChartResourceListView 控件,并使用数据模板在列表视图中显示自定义列:
<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) 的数据上下文设置为我的视图模型类 (TaskData) 的实例。 TaskData 类中有一个 ObservableCollectionint GroupID {get;set;}
和 string GroupName{get;set;}
。
此外,TaskData 类中还有一个 ObservableCollectionGroupID{get;set;}
、字符串 Content {get;set;}
和ResourceGroup ResGroup{get;set;}
上面的代码可以很好地显示组合框和文本框...我一生都无法弄清楚为什么我遇到绑定到组合框的 SelectedValue 属性。我有很多东西,包括 SelectedValue="{Binding GroupID}"
每次我尝试设置 SelectedValue 时,我都会在 VS 中收到此错误弹出窗口: “mscorlib.dll 中发生了类型为‘System.Reflection.AmbigouslyMatchException’的第一次机会异常”这是输出窗口中的错误(其巨大) http://pastebin.com/AGJwn00C
通过阅读,我了解到这是由于父对象具有同名“GroupID”的属性造成的。我已将 Resource 类中的 GroupID 重命名为 ResGroupID,认为它与 ResourceGroup 类冲突,但我收到了相同的错误。
当我设置此 ItemsSource 时,组合框的 DataContext 是否设置为 UserControl 或 TaskData 实例?
更新:
当我使用文本框而不是组合框时,我也会收到错误:
<TextBox Text="{Binding GroupID}"/>
Edit: Rewritting question
I use the Project Management Library from http://dlhsoft.com/Home.aspx in my WPF usercontrol.
I'm displaying their LoadChartResourceListView control on my page and use a datatemplate to display custom columns in a list view:
<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>
The whole user control (inf:MEFUserControl) that contains this LoadChartResourceListView has a datacontext set to an instance of my viewmodel class (TaskData). Within the TaskData class is a ObservableCollection<ResourceGroup> ResourceGroups {get;set;}
. Each ResourceGroup has an int GroupID {get;set;}
and string GroupName{get;set;}
.
Also, within the TaskData class is an ObservableCollection<Resource> Resources {get;set;}
... each Resource has a int GroupID{get;set;}
, string Content {get;set;}
and ResourceGroup ResGroup{get;set;}
The above code works fine with displaying the combobox and the textbox... I cannot, for the life of me, figure out why I'm having issues binding to the SelectedValue property of the combobox. I've many things including SelectedValue="{Binding GroupID}"
Everytime I try to set the SelectedValue I receive this error popup in VS:
"A first chance exception of type 'System.Reflection.AmbiguousMatchException' occurred in mscorlib.dll" This is the error from the output window (its massive) http://pastebin.com/AGJwn00C
From reading, I've read that this is due to a parent object having a property with the same name "GroupID". I've renamed GroupID to ResGroupID in the Resource class, thinking that it conflicted with the ResourceGroup class, but I receive the same error.
When I set this ItemsSource, is the DataContext for the combobox being set to the UserControl or TaskData instance?
Update:
I receive the error also when I use a TextBox instead of a combobox:
<TextBox Text="{Binding GroupID}"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需写入
SelectedValue="{Binding Path=GroupID}"
Just write
SelectedValue="{Binding Path=GroupID}"
解决了。阅读后: http://dlhsoft.com/KnowledgeBase /Task-Appearance-Bar-Templated-Custom-Data-Binding.htm
我必须为自定义属性执行 Item.PropertyName。
Solved it. After reading this: http://dlhsoft.com/KnowledgeBase/Task-Appearance-Bar-Templating-Custom-Data-Binding.htm
I had to do Item.PropertyName for custom properties.