我使用此方法从代码隐藏绑定DataGrid:
private void BindDataGridSetup()
{
//Do datefiltereing first to get left join to work
var datefiltered = data.View_NAV_Entries.Where(e => e.DATO >= this.StartDate && e.DATO <= this.EndDate);
//Linq to get data for "Setup" grid.
var querySetup = from m in data.R_Masterdata
join a in data.View_R_GetAccounts on m.SubsidiaryId equals a.S_Dataset into setup
from accplan in setup.DefaultIfEmpty()
join config in data.R_Configuration on new { MasterDataId = m.MasterDataId, Account = accplan.No_ }
equals new { config.MasterDataId, config.Account } into conf
from x in conf.DefaultIfEmpty()
join component in data.R_Component on new { ComponentId = x.ComponentId, MasterDataId = x.MasterDataId }
equals new { component.ComponentId, this.MasterDataId } into comp
from com in comp.DefaultIfEmpty()
join e in datefiltered
on new { DATASET = accplan.S_Dataset, KONTO = accplan.No_ }
equals new { e.DATASET, e.KONTO } into entries
from ent in entries.DefaultIfEmpty()
where
m.SubsidiaryId.Equals(this.SubsidiaryId)
&& accplan.S_Dataset.Equals(this.SubsidiaryId)
group ent by new { accplan.No_, accplan.Name, x.Eliminate, com.ComponentId, com.ComponentName } into grp
orderby grp.Key.No_
select new
{
AccNumber = grp.Key.No_,
AccName = grp.Key.Name,
Saldo = grp.Sum(e => (e.BELOBDKK == null ? 0 : e.BELOBDKK)),
Component = grp.Key.ComponentName == null ? "Vælg komponent" : grp.Key.ComponentName,
ComponentId = grp.Key.ComponentId == null ? 0 : grp.Key.ComponentId,
Components = data.R_Component.ToList().Where(x => !x.ComponentName.Contains("Blank")),
ConfEliminate = grp.Key.Eliminate
};
try
{
DataGridSetup.ItemsSource = querySetup.ToList();
}
catch(Exception e)
{
System.Windows.MessageBox.Show(e.InnerException.ToString());
}
}
我对这个XAML具有约束力:
<DataGridTemplateColumn x:Name="Component" Header="Komponent">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{ Binding Component }"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="ComboBoxSetup" Height="16"
ItemsSource="{ Binding Components }"
DisplayMemberPath="ComponentName"
SelectedValuePath="{ Binding ComponentId }">
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Padding" Value="1"/>
<Setter Property="Height" Value="19"/>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
文本框显示正常,当我双击ComboBox时,它会随着可能性而下降。一切都很好。 但我无法从代码背后的ComboBox中获取SelectedValue。
如何从ComboBox中获取Selected值?