I'm building a WPF Application using Caliburn.Micro for MVVM. I'm trying to add a client while the application is running but can't get the ComboBox to update/refresh. By printing to the console, I have confirmed that the BindableCollection has an item being added to it, but even though I've included the line:
NotifyOfPropertyChange(() => Clients);
in the Property definition, the ComboBox isn't being updated/refreshed.
Heres the code:
The ComboBox control in ShellView.xaml
<ComboBox Grid.Column="2" Grid.Row="1" MinWidth="200" Margin="0,0,0,5"
x:Name="Clients" SelectedItem="{Binding SelectedClient}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ClientName}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The button control in NewClientView.xaml
<Button x:Name="AddClient" Content="Add Client" Width="100" Margin="5"/>
The Property in ShellViewModel.cs:
public BindableCollection<ClientModel> Clients
{
get
{
return _clients;
}
set
{
_clients = value;
NotifyOfPropertyChange(() => Clients);
}
}
The back-code for the button in NewClientViewModel.cs (a user control for adding a new client):
public void AddClient(BindableCollection<ClientModel> Clients)
{
DataAccess.Clients.Add(new ClientModel
{
ClientName = ClientNameInput,
ClientProducts = null
});
Clients = new BindableCollection<ClientModel>(DataAccess.Clients);
TryClose(true);
}
Any ideas? Thanks.
When I try to add the client directly with this code:
public void AddClient(BindableCollection<ClientModel> Clients)
{
Clients.Add(new ClientModel
{
ClientName = ClientNameInput,
ClientProducts = null
});
TryClose(true);
}
it throws a 'System.Reflection.TargetInvocationException' - Inner Exception 1: NullReferenceException: Object reference not set to an instance of an object.
The constructor for my ShellViewModel:
public ShellViewModel()
{
Clients = new BindableCollection<ClientModel>(GenerateTestData());
}
GenerateTestData() returns a List of type ClientModel.
The call to open the UserControl to add a new client:
public void LoadNewClient()
{
ActivateItem(new NewClientViewModel());
}
Use the EventAggregator to pass variables between ViewModels. I can explain in more detail if the standard documentation isn't enough.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.