I have DataGrid
filled from ObservableCollection
and I have binded Buttons
but the same solution does not work with ComboBox
. I already tried few fixes but every time ComboBox
is empty inside.
XAML:
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Monday" Width="auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<ComboBox x:Name="comboBoxShift" Margin="10,0,0,0" DisplayMemberPath="{Binding Path=Shifts_value}" SelectedValuePath="{Binding Path=Shifts_id}" SelectedValue="{Binding Path=Shifts_selected}" VerticalAlignment="Top" Height="25" Width="auto" FontSize="10" DropDownClosed="comboBoxShift_DropDownClosed">
</ComboBox>
<Button Name="ButtonStandby" Margin="10 0 0 0" Content="Standby" Height="25" Width="auto" IsEnabled="True" FontSize="10" FontWeight="UltraBold" Background="{Binding Path=day1_f_standby}">
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Binding in code:
public class CalendarGlobals
{
public static ObservableCollection<Person> currentTeamOC { get; set; }
}
public TeamScheduleWindow()
{
InitializeComponent();
dataGrid.ItemsSource = CalendarGlobals.currentTeamOC;
}
ObservableCollection code:
public class Person
{
public int day1_id_ca { get; set; }
public int day1_f_shift { get; set; }
public string day1_f_wfh { get; set; }
public string day1_f_standby { get; set; }
public int day1_f_edited { get; set; }
public string day1_note { get; set; }
public DataTable Shifts { get; set; }
public List<string> Shifts_id { get; set; }
public List<string> Shifts_value { get; set; }
public List<string> Shifts_color { get; set; }
public string Shifts_selected { get; set; }
public Person(DataTable day1, DataTable shifts)
{
string[,] colors = new string[,]
{
{"Bisque", "BlueViolet"},
{"Bisque", "BlueViolet"},
};
foreach (DataRow row in day1.Rows)
{
this.day1_id_ca = Convert.ToInt32(row["id_ca"]);
this.day1_f_shift = Convert.ToInt16(row["field_shift"]);
this.day1_f_wfh = colors[0,Convert.ToInt16(row["field_wfh"])];
this.day1_f_standby = colors[1, Convert.ToInt16(row["field_standby"])];
this.day1_f_edited = Convert.ToInt16(row["edited_by_user_id"]);
this.day1_note = row["note"].ToString();
}
//I tried to bind from DataTable, later from list - nothing worked.
this.Shifts = shifts;
this.Shifts_id = shifts.AsEnumerable().Select(x => x[0].ToString()).ToList();
this.Shifts_value = shifts.AsEnumerable().Select(x => x[1].ToString()).ToList();
this.Shifts_color = shifts.AsEnumerable().Select(x => x[2].ToString()).ToList();
}
}
I've removed irrelevant code to make this post shorter. Thank you in advance for any help.
EDIT: Thanks @MKloster for help
I have used your solution with success but I have added new objects to list in ObservableCollection code
Shifts = new BindingList<Shift> ();
foreach (DataRow row in shifts.Rows)
{
Shifts.Add(new Shift { Value = row["value"].ToString(), ID = Convert.ToInt16(row["id_value"]), Color = row["color"].ToString() });
}