通过线程 (backgroundWorker) 将 DataGrid 与列表同步。 WPF C#
Synchronice a DataGrid with a list by a Thread (backgroundWorker). WPF C#
我正在尝试 link 从 List 到 DataGrid 的数据,但是当我启动对象 BackgroundWorker 时,在 BackgroundWorker 填充 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) 并保存它们。
我尝试通过这种方法 link,但它是静态的。
```
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>
我认为不需要静态成员,每个 window 都应该有自己的数据和自己的事件处理程序,所以你应该让它们成为非静态的
UpdatedListProducts
from name 表明该列表经常变化,因此最好将其设为 ObservableCollection<ApiProduct>
而不是 List<ApiProduct>
类型以通知 UI 如果项目添加或删除并反映 UI
中的更改
这里有一篇关于 how to make UI Responding to changes 的好文章,其中介绍了 ObservableCollection 和 INotifyPropertyChanged 接口。
我正在尝试 link 从 List 到 DataGrid 的数据,但是当我启动对象 BackgroundWorker 时,在 BackgroundWorker 填充 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) 并保存它们。 我尝试通过这种方法 link,但它是静态的。 ```
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>
我认为不需要静态成员,每个 window 都应该有自己的数据和自己的事件处理程序,所以你应该让它们成为非静态的
UpdatedListProducts
from name 表明该列表经常变化,因此最好将其设为 ObservableCollection<ApiProduct>
而不是 List<ApiProduct>
类型以通知 UI 如果项目添加或删除并反映 UI
这里有一篇关于 how to make UI Responding to changes 的好文章,其中介绍了 ObservableCollection 和 INotifyPropertyChanged 接口。