WPF DataTemplate ComboBox binding issue
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}"/>
Just write
SelectedValue="{Binding Path=GroupID}"
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.
链接地址: http://www.djcxy.com/p/44618.html上一篇: 在自动完成组合框中绑定文本属性