I have a DatagridTemplateColumn
using a ComboBox
, but the ItemSource
will not fill with the bound collection.
I should mention that the DataGrid
is being correctly bound and any other collections in the veiwModel is working, it is just this ComboBox
in the datagrid does not work.
This is the MCVE sample code:
<UserControl
d:DataContext="{d:DesignInstance d:Type=viewModels:StaffInfoDetailViewModel, IsDesignTimeCreatable=False}">
<DataGrid Grid.Column="0" Grid.ColumnSpan="6" AutoGenerateColumns="False" ItemsSource="{Binding SectionStaffMasterDisplay}" Grid.Row="4" Grid.RowSpan="2" AlternationCount="2" CanUserAddRows="True" CanUserDeleteRows="True" GridLinesVisibility="None" VerticalAlignment="Top" CanUserSortColumns="False">
<DataGridTemplateColumn Width="190" Header="資格">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
DisplayMemberPath="ItemName"
SelectedValuePath="ItemName"
SelectedItem="{Binding Path=Name, UpdateSourceTrigger=LostFocus}"
ItemsSource="{Binding Path=LicenceComboBox}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
.....more XAML
And the ViewModel
public class StaffInfoDetailViewModel : CollectionViewModel<StaffInfoDetailWrapper>
{
public StaffInfoDetailViewModel()
{
LicenceComboBoxItems();
MasterDataDisplay();
}
public void LicenceComboBoxItems()
{
foreach (var item in DataProvider.StartUpSection)
{
LicenceComboBox.Add(item);
}
}
private ObservableCollection<Licence> _licenceComboBox = new ObservableCollection<Licence>();
public ObservableCollection<Licence> LicenceComboBox
{
get { return _licenceComboBox; }
set
{
_licenceComboBox = value;
OnPropertyChanged();
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged();
}
}
The Model class:
public partial class Licence
{
public System.Guid Id { get; set; } // ID (Primary key)
public string ItemName { get; set; } // ItemName (length: 50)
public string Section { get; set; } // Section (length: 50)
public Licence()
{
InitializePartial();
}
partial void InitializePartial();
}
The datagrid collection.
private ObservableCollectionEx<StaffInfoDetail> _sectionStaffMasterDisplay = new ObservableCollectionEx<StaffInfoDetail>();
public ObservableCollectionEx<StaffInfoDetail> SectionStaffMasterDisplay
{
get { return _sectionStaffMasterDisplay; }
set
{
if (value != _sectionStaffMasterDisplay)
{
_sectionStaffMasterDisplay = value;
OnPropertyChanged();
}
}
}
The Entity class that the collection is filled by,
public partial class StaffInfoDetail
{
public System.Guid Id { get; set; } // ID (Primary key)
public byte[] Image { get; set; } // Image (length: 2147483647)
public int? StaffNo { get; set; } // StaffNo
public string SecondName { get; set; } // SecondName (length: 50)
public string FirstName { get; set; } // FirstName (length: 50)
public string Section { get; set; } // Section (length: 50)
public string SubSection { get; set; } // SubSection (length: 50)
public string Licence { get; set; } // Licence (length: 50)
public System.DateTime? StartDate { get; set; } // StartDate
public System.DateTime? EndDate { get; set; } // EndDate
public long? NightShiftOne { get; set; } // NightShiftOne
public long? NightShiftTwo { get; set; } // NightShiftTwo
public long? Lunch { get; set; } // Lunch
public long? Unplesant { get; set; } // Unplesant
public string JobType { get; set; } // JobType (length: 50)
public bool Kaizen { get; set; } // Kaizen
public int KaizenPercentage { get; set; } // KaizenPercentage
public bool? CurrentStaff { get; set; } // CurrentStaff
public string Notes { get; set; } // Notes (length: 4000)
public StaffInfoDetail()
{
InitializePartial();
}
partial void InitializePartial();
}
And the method that fills the collection, I added the caller to the original code from public StaffInfoDetailViewModel()
:
public void MasterDataDisplay()
{
SectionStaffMasterDisplay.AddRange(DataProvider.StaffInfos.Where(p =>
p.CurrentStaff == true &&
DataProvider.StartUpSection.Contains(p.Section)).ToObservable());
}
I can't see an issue with the DataContext
,but why won't this bind correctly when every other property is working?
And stepping through the code shows LicenceComboBox
to be correctly filled.