Hello Community,
In my Application, I have two comboboxes. The second one depends on the value of the previous Selected Value. But the cb2 is not updated when I select something in cb1.
When I start my Application the cb1 is populated properly with all Clients that exist in my Database (Binding with EnitiyFramework works) and when I select a value there, the property "ClientForwarders" is updated to all forwarders that belong to the client selected, BUT the combobox does not show them.
My Model:
public class Client
{
public int ClientId { get; set; }
public string ClientName { get; set; }
public virtual ICollection<CommandMapping> CommandMappings { get; set; }
}
public class Forwarder
{
public int ForwarderId { get; set; }
public string ForwarderName { get; set; }
public virtual ICollection<CommandMapping> CommandMappings { get; set; }
}
public class CommandMapping
{
public int CommandMappingId { get; set; }
public string CommandMappingName { get; set; }
public virtual Client Client { get; set; }
public virtual Forwarder Forwarder { get; set; }
}
View:
<Window [...]>
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<ComboBox Name="cb1" ItemsSource="{Binding Clients, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="ClientName" SelectedItem="{Binding SelectedClient}"/>
<ComboBox Name="cb2" ItemsSource="{Binding ClientForwarders, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="ForwarderName" SelectedItem="{Binding SelectedForwarder}"/>
</Window>
And my ViewModel:
namespace ProjectXY.ViewModel
{
public class MainWindowViewModel : INotifyPropertyChanged
{
#region ClientHandling
public IEnumerable<Client> Clients { get; private set; }
private Client _selectedClient;
public Client SelectedClient
{
get => _selectedClient;
set
{
_selectedClient = value;
OnPropertyChanged(nameof(SelectedClient));
}
}
#endregion
#region ForwarderHandling
public IEnumerable<Forwarder> ClientForwarders { get; private set; }
private Forwarder _selectedForwarder;
public Forwarder SelectedForwarder
{
get => _selectedForwarder;
set
{
_selectedForwarder = value;
OnPropertyChanged(nameof(SelectedForwarder));
}
}
#endregion
private EFDataModel _ctx;
public MainWindowViewModel()
{
LoadData();
}
protected void LoadData()
{
Refresh();
}
public void Refresh()
{
_ctx = new DataModel();
_ctx.Clients.Load();
_ctx.Forwarders.Load();
_ctx.CommandMappings.Load();
Clients = _ctx.Clients.Local.ToList();
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
switch (propertyName)
{
case null:
return;
case nameof(SelectedClient):
var forwarderIdList = new List<int>();
Task.WaitAll(_ctx.CommandMappings
.Where(cm => cm.Client.ClientId == SelectedClient.ClientId)
.ForEachAsync(x => forwarderIdList.Add(x.Forwarder.ForwarderId)));
ClientForwarders = _ctx.Forwarders.Local.Where(x => forwarderIdList.Contains(x.ForwarderId)).ToList();
break;
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
What I have tried:
For Debugging I filled the "ClientForwarders" property directly in the "Refresh()" method. That leads to (as expected) all forwarders are being shown in cb2.