My goal is to have a set of cascading comboboxes in WPF. I'm trying to use the MVVM model, but still learning.
Some background information on the project. I'm trying to edit times for employees.
So I have a list of Times for the selected employee in a DataGrid. Each row in the DataGrid is a Time object. A Time consists of some fields InTime, OutTime, Date, Hours... ect. A Time also has a Department and a Job.
Currently I have the Department ComboBox wired up and working, but I'm not sure how to build the Job combobox based on what is selected in the Department field.
Here is how my ViewModel is set up
public ObservableCollection<Time> Times { get; set; }
public ObservableCollection<Department> Departments { get; set; }
public TimeSheetsViewModel()
{
Times = new ObservableCollection<Time>();
Departments = new ObservableCollection<Departments>();
GetDepartments();
}
private void GetDepartments()
{
/*
This section contains code to connect to my SQL Database and fills a DataTable dt
*/
if (Departments != null)
Departments.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
Department d = new Department() { Display = dt.Rows[i]["DISPLAY"].ToString(), DepartmentCode = dt.Rows[i]["DEPARTMENT_CODE"].ToString(), CompanyCode = dt.Rows[i]["COMPANY_CODE"].ToString() };
Departments.Add(d);
}
}
This is the binding on my DataGrid
<DataGrid Grid.Row="1" Margin="15,0,15,15" Visibility="Visible" FontSize="14" HorizontalGridLinesBrush="{StaticResource Nelson2}" VerticalGridLinesBrush="{StaticResource Nelson2}" ItemsSource="{Binding Times}" SelectionMode="Single" CellEditEnding="DataGrid_CellEditEnding" RowEditEnding="DataGrid_RowEditEnding" AutoGenerateColumns="False">
<DataGridTemplateColumn Header="Department Code">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path= Department.Display}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Findancestor, AncestorType={x:Type UserControl}}, Path=DataContext.Departments}" DisplayMemberPath="Display" SelectedValuePath="DepartmentCode" SelectedValue="{Binding Department.DepartmentCode}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid>
So how do I implement my job combobox to populate its items based on whatever is selected for department in that row?
I'm assuming I want to put the code for this in my same view model.
Any help is appreciated, Thanks!
EDIT (04/05/16):
How can I return an Object with a Converter so that I can use that Converter to bind different things to fields of that Object.
Say this is my converter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string departmentCode = values[0].ToString();
ObservableCollection<Department> Departments = values[1] as ObservableCollection<Department>;
return Departments.FirstOrDefault(Department => Department.DepartmentCode == departmentCode);
}
And this is my binding
<TextBlock >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource DeptCodeToDeptConverter}" >
<Binding Path="DepartmentCode" />
<Binding Path="DataContext.Departments" RelativeSource="{RelativeSource Findancestor, AncestorType={x:Type UserControl}}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
That converter will return a Department Object, but what if I want the TextBlock's Text to be Department.Name or Department.Location. Do I have to create a new converter to return each of the field I want to use in different controls? Or is there a way to achieve what I want using this method?