首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用单独的ItemsSource在加载时绑定到数据的WPF DataGrid组合框

使用单独的ItemsSource在加载时绑定到数据的WPF DataGrid组合框
EN

Stack Overflow用户
提问于 2019-01-10 04:17:58
回答 1查看 515关注 0票数 0

我有一点左右为难,我还没能解决,至少从MVVM的角度来看,不是用一种合理的方法。

我有一个datagrid,其中包含每个客户/员工关系的开始日期和结束日期的客户-员工数据。

DataGrid总体ItemsSource是绑定到ClientToEmpObservableCollection的ClientToEmp CollectionViewSource。但是,在允许他们更改/更新当前员工的组合框列中,ItemsSource是绑定到用户ObservableCollection(即,他们可以为该客户端选择的所有员工的列表)的员工CollectionViewSource。

这部分运行良好,当我单击组合框时,将在组合框中显示适当的员工以供选择。但是,当数据网格加载时,我希望ClientToEmp cvs中的CurrentEmp显示为选定的员工(即,当前分配给此客户端的员工)。当他们点击它时,他们应该能够更改员工(从单独的员工简历),这将更新ClientToEmp简历中的值。

代码语言:javascript
运行
复制
<DataGrid Name="ClientToEmpMDG" ItemsSource="{Binding cvsClientToEmp}" 
                      AutoGenerateColumns="False" AutoGeneratingColumn="Gen_AutoGridColumns">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding ClientName}" Header="Client Name" IsReadOnly="True"/>
                    <DataGridTemplateColumn Header="Current Emp">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
                                          DisplayMemberPath="DisplayName"
                                          SelectedValuePath="User_ID"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>

                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Start Date">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <DatePicker Name="StartDateDP" SelectedDate="{Binding Path=Start_Date}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="End Date">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <DatePicker Name="EndDateDP" SelectedDate="{Binding Path=End_Date}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>

如何让当前员工在加载时正确显示?选定的值应与两个ObservableCollections中的User_ID匹配。如果我使用SelectedValue属性,网格中的所有组合框都显示相同的值,但它仍然不能正确地绑定到我希望它绑定到的cvsClientToEmp值。我希望每个组合框都有自己的值,而不是所有的组合框都共享一个值。

更新:我修复了所有组合框显示相同值的问题,将"IsSynchronizedWithCurrentItem“添加到False...Still无法将其绑定到从数据库返回的当前员工的值

EN

回答 1

Stack Overflow用户

发布于 2019-01-10 04:29:21

您的ComboBox中缺少SelectedValue

代码语言:javascript
运行
复制
<ComboBox ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
    DisplayMemberPath="DisplayName"
    SelectedValue="{Binding CurrentEmp}"
    SelectedValuePath="User_ID"/>

另请参阅https://stackoverflow.com/a/4902454/10718884以获得更好的解释

注意:还有一个DataGridComboBoxColumn

代码语言:javascript
运行
复制
<DataGridComboBoxColumn
    ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
    DisplayMemberPath="DisplayName"
    SelectedValueBinding="{Binding CurrentEmp}"
    SelectedValuePath="User_ID">
</DataGridComboBoxColumn>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54117682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档