I am using a DataGrid to allow editing of stored server info, one of the columns is a delete button to delete the server from the bound BindingList for the DataGrid.ItemsSource but when the server is removed from the BindingList the DataGrid is not refreshed until you click on another entry to trigger a selection. See relevant code below:
ViewModel
public partial class ManageServersDialogViewModel : DialogViewModelBase
{
private BindingList<Server> mServersLocal = new BindingList<Server>();
public BindingList<Server> ServersLocal
{
get => mServersLocal;
set
{
SetProperty(ref mServersLocal, value);
}
}
[RelayCommand]
private void Delete(Server iServer)
{
ServersLocal.Remove(iServer);
OnPropertyChanged(nameof(ServersLocal));
{
}
View
<DockPanel LastChildFill="True">
<DockPanel DockPanel.Dock="Bottom" LastChildFill="True">
<!-- Other Stuff -->
</DockPanel>
<DataGrid
x:Name="ServersDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding ServersLocal, Mode=TwoWay}"
SelectedItem="{Binding SelectedServer, Mode=TwoWay}"
SelectionChanged="DataGrid_SelectionChanged"
SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn
Width="*"
Binding="{Binding Name}"
Header="Name" />
<DataGridTextColumn
Width="250"
Binding="{Binding Address}"
Header="Address" />
<DataGridTextColumn
Width="70"
Binding="{Binding Port}"
Header="Port" />
<DataGridTextColumn
Width="200"
Binding="{Binding Password}"
Header="Password" />
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Command="{Binding #ZSMManageServersDialog.((local:ManageServersDialogViewModel)DataContext).ConnectCommand}"
CommandParameter="{Binding .}"
Content="Connect" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Command="{Binding #ZSMManageServersDialog.((local:ManageServersDialogViewModel)DataContext).DeleteCommand}"
CommandParameter="{Binding .}"
Content="Delete" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
I am using a DataGrid to allow editing of stored server info, one of the columns is a delete button to delete the server from the bound BindingList for the DataGrid.ItemsSource but when the server is removed from the BindingList the DataGrid is not refreshed until you click on another entry to trigger a selection. See relevant code below:
ViewModel
public partial class ManageServersDialogViewModel : DialogViewModelBase
{
private BindingList<Server> mServersLocal = new BindingList<Server>();
public BindingList<Server> ServersLocal
{
get => mServersLocal;
set
{
SetProperty(ref mServersLocal, value);
}
}
[RelayCommand]
private void Delete(Server iServer)
{
ServersLocal.Remove(iServer);
OnPropertyChanged(nameof(ServersLocal));
{
}
View
<DockPanel LastChildFill="True">
<DockPanel DockPanel.Dock="Bottom" LastChildFill="True">
<!-- Other Stuff -->
</DockPanel>
<DataGrid
x:Name="ServersDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding ServersLocal, Mode=TwoWay}"
SelectedItem="{Binding SelectedServer, Mode=TwoWay}"
SelectionChanged="DataGrid_SelectionChanged"
SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn
Width="*"
Binding="{Binding Name}"
Header="Name" />
<DataGridTextColumn
Width="250"
Binding="{Binding Address}"
Header="Address" />
<DataGridTextColumn
Width="70"
Binding="{Binding Port}"
Header="Port" />
<DataGridTextColumn
Width="200"
Binding="{Binding Password}"
Header="Password" />
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Command="{Binding #ZSMManageServersDialog.((local:ManageServersDialogViewModel)DataContext).ConnectCommand}"
CommandParameter="{Binding .}"
Content="Connect" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Command="{Binding #ZSMManageServersDialog.((local:ManageServersDialogViewModel)DataContext).DeleteCommand}"
CommandParameter="{Binding .}"
Content="Delete" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
Share
Improve this question
edited Feb 1 at 17:26
OpticalMagician
asked Jan 23 at 12:41
OpticalMagicianOpticalMagician
1031 silver badge12 bronze badges
1 Answer
Reset to default 0If you haven’t tried this yet, you might consider using ObservableCollection instead of BindingList.
Depending on how your view is set up, it could be a simple drop-in replacement. Since ObservableCollection implements INotifyCollectionChanged, it might work better for data binding in Avalonia, I mean, it’s worth a shot at least.
Also, I found this example: How to Bind to a Collection.