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

时间:2016-04-01 15:00:29

标签: c# wpf mvvm datagrid prism-5

我正在开发一个自定义WPF MVVM(Prism)应用程序,并且在ComboBox内遇到DataGrid绑定时遇到问题。我见过其他有类似问题的人,但没有什么相同的,我无法得到任何100%的工作答案。

对于这个特定的观点,我代表一个父子关系。在显示表单时,我在基本Grid的顶部显示父信息,这些绑定工作正常。下面是DataGrid,其中1:与该父记录相关的项目很多。它非常简单 - 仅表示显示顺序的TextBox,然后是保持统计类型值的ComboBoxComboBox绑定到ObservableCollection,我可以让它正确显示这些值;但是,我无法让它为给定记录选择正确的值。我能够在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>

视图模型

// 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绑定上,但似乎无法弄清楚我错过了什么。感谢您的任何见解。

1 个答案:

答案 0 :(得分:0)

用于SelectedValueBinding的属性将是一个自定义属性(占位符),用于获取用户选择的值。您对SelectedValuePathSelectedValueBinding使用相同的属性,这是毫无意义的。