簡體   English   中英

通過線程 (backgroundWorker) 將 DataGrid 與列表同步。 WPF C#

[英]Synchronice a DataGrid with a list by a Thread (backgroundWorker). WPF C#

我正在嘗試將數據從列表鏈接到 DataGrid,但是當我啟動 object BackgroundWorker 時,在 BackgroundWorker 填充列表之前已經加載了主線程(帶有一個空列表)。 如何更新或其他東西以從 BackgroundWorker 加載數據?

MainWindow的構造函數,這里我聲明BackgroundWorker

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

調用 function backgroundWorker_DoWork 的方法

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

    }  

如果操作正確,我會得到一個對象列表(ApiProduct)並保存它們。 我嘗試通過這種方法進行鏈接,但它是 static。 ```

  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>

我認為不需要 static 成員,每個 window 都應該有自己的數據和自己的事件處理程序,所以你應該讓它們成為非靜態的

名稱中的UpdatedListProducts顯示列表定期更改,因此最好將其ObservableCollection<ApiProduct>類型而不是List<ApiProduct>以在添加或刪除項目時通知 UI 並在 UI 中反映該更改

這是一篇關於如何通過對 ObservableCollection 和 INotifyPropertyChanged 接口的介紹使 UI 響應更改的好文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM