最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Avalonia DataGrid not refreshing - Stack Overflow

programmeradmin3浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

If 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.

发布评论

评论列表(0)

  1. 暂无评论