WPF DataTemplate ComboBox 绑定问题

Posted

技术标签:

【中文标题】WPF DataTemplate ComboBox 绑定问题【英文标题】:WPF DataTemplate ComboBox binding issue 【发布时间】:2011-07-05 12:25:33 【问题描述】:

编辑:重写问题

我在我的 WPF 用户控件中使用来自 http://dlhsoft.com/Home.aspx 的项目管理库。

我在我的页面上显示他们的 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 类中有一个ObservableCollection&lt;ResourceGroup&gt; ResourceGroups get;set;。每个 ResourceGroup 都有一个 int GroupID get;set;string GroupNameget;set;

此外,TaskData 类中有一个ObservableCollection&lt;Resource&gt; Resources get;set; ...每个资源都有一个int GroupIDget;set;、字符串Content get;set;ResourceGroup ResGroupget;set;

上面的代码可以很好地显示组合框和文本框...我一辈子都无法弄清楚为什么我在绑定到组合框的 SelectedValue 属性时遇到问题。我有很多东西,包括SelectedValue="Binding GroupID"

每次我尝试设置 SelectedValue 时,我都会在 VS 中收到此错误弹出窗口: “在 mscorlib.dll 中发生了 'System.Reflection.AmbiguousMatchException' 类型的第一次机会异常”这是来自输出窗口的错误(其巨大)http://pastebin.com/AGJwn00C

从阅读中,我了解到这是由于父对象具有同名“GroupID”的属性。我在 Resource 类中将 GroupID 重命名为 ResGroupID,认为它与 ResourceGroup 类冲突,但我收到了同样的错误。

当我设置此 ItemsSource 时,组合框的 DataContext 是否设置为 UserControl 或 TaskData 实例?

更新:

当我使用 TextBox 而不是组合框时,我也会收到错误消息:

<TextBox Text="Binding GroupID"/>

【问题讨论】:

Selected 值从何而来?您已将绑定设置为 LoadChartResourceListView 的 GroupID 属性,我认为该属性不存在。您可以转到“输出”窗口并在那里记录绑定错误。 SelectedValue 来自我的 ObservableCollection 中的每个元素...每个元素都通过此 LoadChartResourceListView.View 枚举并显示在屏幕上。老实说...它可能与 LoadChartResourceListView 无关...我只需要知道如何绑定到以下数据模板绑定到的同一对象 - 因为在设置我的组合框的 itemssource 时数据上下文似乎已经改变...我在问题的末尾添加了 2 个新段落 【参考方案1】:

随便写

SelectedValue="Binding Path=GroupID"

【讨论】:

.. 已经尝试过了(正如我在问题中所说),也许它不喜欢 SelectedValue 和 SelectedValuePath 具有相同的名称? 嗨,克里斯,使用 SelectedValue="Binding RelativeSource=RelativeSource FindAncestor, AncestorType=x:Type my:LoadChartResourceListView, Path=SelectedGroupID" 注意:您需要在您的视图模型【参考方案2】:

解决了。看完这篇:http://dlhsoft.com/KnowledgeBase/Task-Appearance-Bar-Templating-Custom-Data-Binding.htm

我必须为自定义属性执行 Item.PropertyName。

【讨论】:

以上是关于WPF DataTemplate ComboBox 绑定问题的主要内容,如果未能解决你的问题,请参考以下文章

WPF ComboBox SelectedItem在TabControl Switch上设置为Null

在WPF中怎么将ComboBox的下拉列表的数据进行绑定?还有能不能TXT文件中的列表?

【WPF】怎样获取DataGrid模板列ComboBox选择的值?

WPF源代码分析系列一:剖析WPF模板机制的内部实现

WPF ComboBox SelectedItem 在 TabControl 开关上设置为 Null

具有空值的 WPF ComboBox SelectedValue 绑定显示为空白