WPF MVVM 应用程序中的 DataGridComboBoxColumn 绑定问题

DataGridComboBoxColumn binding issues in WPF MVVM application

我有一个正在开发的自定义 WPF MVVM (Prism) 应用程序,但在 DataGrid 中使用 ComboBox 绑定时遇到了一些问题。我见过其他人也有类似的问题,但没有什么完全相同的,我无法得到任何 100% 适合我的情况的答案。

对于这个特定的观点,我代表的是亲子关系。显示表单时,我在顶部以基本 Grid 显示父信息,这些绑定工作正常。下面是 DataGrid,其中 1:many 项与该父记录相关。它非常简单 - 只是一个 TextBox 表示显示顺序,然后是一个 ComboBox 保存统计类型值。 ComboBox 绑定到 ObservableCollection,我可以让它正确显示这些值;但是,我无法将其设为 select 给定记录的正确值。我能够在 TextBox 中显示正确的值,所以我知道这些值是正确的,我只是无法在 ComboBox.

上获得正确的绑定

这是相关代码:

查看:

<DataGrid AutoGenerateColumns="false" Name="DataGridStatistics" Width="900" 
          ItemsSource="{Binding Path=DspTemplateStats, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                  >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Display Order" Width="100"
                            Binding="{Binding Path=DisplayOrder, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
        <!-- This is where the issue is.  It is displaying the StatisticTypeName properly in the combobox, but it is not connecting the StatisticId
        from the DspTemplateStats collection to the StatisticTypes collection -->
        <DataGridComboBoxColumn Header="Statistic Type 2"
                                SelectedValueBinding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.StatisticId}"
                                SelectedValuePath="StatisticId"
                                DisplayMemberPath="StatisticTypeName"
                                >
             <DataGridComboBoxColumn.ElementStyle>
                 <Style TargetType="ComboBox">
                     <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatisticTypes}"/>
                 </Style>
             </DataGridComboBoxColumn.ElementStyle>
             <DataGridComboBoxColumn.EditingElementStyle>
                 <Style TargetType="ComboBox">
                     <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatisticTypes}"/>
                     </Style>
             </DataGridComboBoxColumn.EditingElementStyle>
         </DataGridComboBoxColumn>
         <!-- This is temporary to test the binding.  It displays the correct value -->
         <DataGridTextColumn Header="Statistic" Width="*" Binding="{Binding Path=StatisticTypeName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
     </DataGrid.Columns>
 </DataGrid>

ViewModel

// This is how DspTemplateStats is defined, populated from Stored Procedure
// I pass it the ProejctTemplateId and it retrieves all the Statistics associated with it
private ObservableCollection<ProjectTemplateStat> dspTemplateStats;
public ObservableCollection<ProjectTemplateStat> DspTemplateStats
{
    get { return dspTemplateStats; }
    set
    {
        dspTemplateStats = value;
        OnPropertyChanged("DspTemplateStats");
    }
}

// This is how StatisticTypes is defined, populated from Stored Procedure
// It pulls back all StatisticTypes defined in the database
private ObservableCollection<StatisticType> statisticTypes;
public ObservableCollection<StatisticType> StatisticTypes
{
    get { return statisticTypes; }
    set
    {
        statisticTypes = value;
        OnPropertyChanged("StatisticTypes");
    }
}

和关键 型号

public class ProjectTemplateStat
{
    public int ProjectTemplateId { get; set; }
    public int TemplateStatId { get; set; }
    public int StatisticId { get; set; }
    public string TemplateName { get; set; }
    public string StatisticTypeName { get; set; }
    public int DisplayOrder { get; set; }
}

// The match should between StatisticType.StatisticTypeId and ProjectTemplateStat.StatisticId
public class StatisticType
{
    public int StatisticTypeId { get; set; }
    public int UOMId { get; set; }
    public string UOMValue { get; set; }
    public int DatatypeId { get; set; }
    ...
}

我觉得问题出在 DataGridComboBoxColumn 绑定上,但似乎无法弄清楚我遗漏了什么。感谢您的任何见解。

用于 SelectedValueBinding

属性 将是自定义 属性(占位符)以获取用户选择的值。您对 SelectedValuePathSelectedValueBinding 使用相同的 属性,这是没有意义的。