我有一个正在开发的自定义 WPF MVVM (Prism) 应用程序,但ComboBox
在DataGrid
. 我见过其他人有类似的问题,但没有什么完全相同的,我无法得到任何答案来 100% 解决我的情况。
对于这个特定的观点,我代表的是亲子关系。显示表单时,我在基本信息的顶部显示父信息,Grid
并且这些绑定工作正常。下面是DataGrid
与该父记录相关的 1:many 项目。这很简单 - 只是一个TextBox
表示显示顺序,然后是一个ComboBox
持有统计类型值。ComboBox
绑定到 an ObservableCollection
,我可以让它正确显示这些值;但是,我无法让它为给定记录选择正确的值。我能够在 a 中显示正确的值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
绑定上,但似乎无法弄清楚我缺少什么。感谢您的任何见解。