【问题标题】:WPF DataGrid Combobox binding to data on load with separate ItemsSourceWPF DataGrid Combobox 绑定到加载时使用单独的 ItemsSource 的数据
【发布时间】:2019-06-04 16:09:46
【问题描述】:

我有一点我无法解决的难题,至少从 MVVM 的角度来看,这不是一种有意义的方法。

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

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

这部分工作正常,当我单击组合框时,将显示适当的员工以在组合框中进行选择。但是,当数据网格加载时,我希望 ClientToEmp cvs 中的 CurrentEmp 显示为选定的员工(即当前分配给该客户端的员工)。当他们点击它时,他们应该能够更改 Employee(来自单独的员工 cvs),然后会更新 ClientToEmp cvs 中的值。

<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,解决了所有组合框显示相同值的问题...仍然无法将其绑定到从数据库返回的值,以了解当前员工是谁

【问题讨论】:

  • 我在跟进时遇到了一些麻烦。您能否添加一张图片来说明您的意思?
  • @Selthien 我无法添加图片,但基本上我有一个 DataGrid,我将数据从服务器加载到 ObservableCollection 中,填充客户到员工的关系。 User_ID、CurrentEmp、ClientName、StartDate、EndDate 是这些字段。 “Current Emp”列是一个组合框列,包含所有员工的组合框项目(项目来源与 DataGrid 不同)。我希望每一行自动选择数据网格使用的 ClientToEmp ItemsSource 中的 CurrentEmp 作为选定值(即,在加载时在组合框中显示它)。我无法正常工作。
  • @MattE “CurrentEmp”属于哪种类型?与 ComboBox-Source 中某个项目的“User_ID”相同?

标签: c# wpf xaml combobox datagrid


【解决方案1】:

您的ComboBox 中缺少SelectedValue

<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

<DataGridComboBoxColumn
    ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
    DisplayMemberPath="DisplayName"
    SelectedValueBinding="{Binding CurrentEmp}"
    SelectedValuePath="User_ID">
</DataGridComboBoxColumn>

【讨论】:

  • 不起作用...已经尝试过了...问题是我需要让它绑定到数据网格行的 cvsClientToEmp User_ID 值而不是组合框绑定到的 cvsEmp ItemsSource ...它还将更改数据网格中的每个组合框以具有此值,而不仅仅是我更改的单个组合框。此外,如果我使用 DataGridComboBoxColumn,我需要编辑 ComboBox 的 ElementStyle 和 EditedElementStyle ...您的解决方案对此无效
  • @MattE 好吧,这很奇怪,因为SelectedValue 对应于该行的项目。
  • 我什至尝试过 SelectedValue="{Binding Source=cvsClientToEmp, Path=User_ID}" 仍然是同样的问题...
  • @MattE 根据您对属性的评论编辑我的答案。只要CurrentEmp(行中的项目)的类型可以转换为User_ID(组合框中的项目)的类型,这应该可以工作
  • 需要这样,拿不到。
猜你喜欢
  • 2020-01-30
  • 2015-04-07
  • 2011-05-06
  • 2013-06-30
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
相关资源
最近更新 更多