【问题标题】:Synchronice a DataGrid with a list by a Thread (backgroundWorker). WPF C#通过线程 (backgroundWorker) 将 DataGrid 与列表同步。 WPF C#
【发布时间】:2021-07-09 07:28:06
【问题描述】:

我想将数据从 List 链接到 DataGrid,但是当我启动对象 BackgroundWorker 时,在 BackgroundWorker 填充 List 之前已经加载了主线程(带有一个空 List)。 如何更新或其他东西以从 BackgroundWorker 加载数据?

MainWindow的构造函数,这里我声明的是BackgroundWorker

```
  public MainWindow()
    {
        InitializeComponent();
        backgroundWorker = new BackgroundWorker();            
    }   

调用函数backgroundWorker_DoWork的方法

```       
  private void referenceBtn_Click(object sender, RoutedEventArgs e)
     {
        backgroundWorker.DoWork += backgroundWorker_DoWork;
        backgroundWorker.RunWorkerAsync();

    }  

如果操作正确,我会得到一个对象列表(ApiProduct)并保存它们。 我尝试通过这种方法链接,但它是静态的。 ```

  public static void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        if (textinPut != null && textinPut.Length >= 3)
        {
            ApiConector apiConector = new ApiConector();
            UpdatedListProducts = apiConector.getProductList(textinPut, 2);
            //dgProducto.ItemsSource = UpdatedListProducts;
        }
    } 

```

 public static List<ApiProduct> UpdatedListProducts
    {
        get { return apiProductsStatic.ToList(); }
        set
        {
            apiProductsStatic = value;
        }
    } 

```

 <Grid HorizontalAlignment="Center" Margin="0,20,0,0"  Width="Auto">
                <DataGrid AutoGenerateColumns="False"  x:Name="dgProducto" CanUserAddRows="False" 
                    ColumnWidth="Auto" IsReadOnly="True" 
                          SelectedCellsChanged="dgProducto_SelectedCellsChanged"
                          EnableColumnVirtualization = "True" EnableRowVirtualization = "True"  
                         ScrollViewer.CanContentScroll ="False" DataGrid.RowHeight ="75" 
                           VerticalAlignment="Top" Width="auto"
                         >
                    <DataGrid.Columns  >
                        <DataGridTemplateColumn Header="IMAGEN">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Image Source="{Binding Image}" Height="40" Width="40" 
                                     VerticalAlignment="Center"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn >
                        <DataGridTextColumn Header="NOMBRE" Binding="{Binding NameWhole}" />
                        <DataGridTextColumn Header="REFERENCIA" Binding="{Binding Reference}" />
                        <DataGridTextColumn Header="UBICACION" Binding="{Binding Location}" />
                        <DataGridTextColumn Header="PRECIO" Binding="{Binding Price}" />
                        <DataGridTemplateColumn Header="">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <Button x:Name="imprimirBtn" Content="Imprimir" 
                                        Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" 
                                        Style="{StaticResource MaterialDesignFlatButton}" 
                                               Click="imprimirBtn_Click"  />
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>

【问题讨论】:

  • 那你为什么把backgroundWorker_DoWork方法设为静态呢?删除static..

标签: c# wpf visual-studio datagrid


【解决方案1】:

我认为不需要静态成员,每个窗口都应该有自己的数据和自己的事件处理程序,所以你应该让它们成为非静态的

UpdatedListProducts from name 表明该列表经常更改,因此最好将其设为 ObservableCollection&lt;ApiProduct&gt; 而不是 List&lt;ApiProduct&gt; 类型,以便在添加或删除项目时通知 UI 并在 UI 中反映该更改

这是一篇关于 how to make UI Responding to changes 的好文章,其中介绍了 ObservableCollection 和 INotifyPropertyChanged 接口。

【讨论】:

  • 谢谢,我将 List 更改为 ObservableCollection 并删除了静态句子。我还使用 Dispatcher.Invoke 与主线程一起工作,它运行良好。
猜你喜欢
  • 2010-09-29
  • 2023-03-19
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多