C# 将ObservableCollection对象绑定到DataGrid c.net
我在WPF中遇到了一个问题 我得到了一个数据网格,比如:C# 将ObservableCollection对象绑定到DataGrid c.net,c#,.net,wpf,C#,.net,Wpf,我在WPF中遇到了一个问题 我得到了一个数据网格,比如: DataGrid Grid.Row="0" x:Name="DataGridOverview" MinHeight="250" MinWidth="705" ItemsSource="{Binding}" Style="{StaticResource DataGridStyle}" > <DataGrid.Columns>
DataGrid Grid.Row="0" x:Name="DataGridOverview" MinHeight="250" MinWidth="705"
ItemsSource="{Binding}" Style="{StaticResource DataGridStyle}" >
<DataGrid.Columns>
<DataGridTextColumn Width="*" MinWidth="100" Header="Name" Binding="{Binding (clientsideFileManager:CfmModel+FileInformationForOverview.Name)}" />
<DataGridTextColumn Width="1*" MinWidth="50" Header="Größe" Binding="{Binding (clientsideFileManager:CfmModel+FileInformationForOverview.HumanReadableSize)}" />
<DataGridTextColumn Width="1*" MinWidth="70" Header="Typ" Binding="{Binding (clientsideFileManager:CfmModel+FileInformationForOverview.Type)}" />
<DataGridTextColumn Width="1*" MinWidth="70" Header="Status" Binding="{Binding (clientsideFileManager:CfmModel+FileInformationForOverview.Status)}"/>
<DataGridTemplateColumn Header="Progress" Width="1*" MinWidth="70">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ProgressBar Name="progressBar"
Value="{Binding Path=(clientsideFileManager:CfmModel+FileInformationForOverview.ProgressValue), Mode=OneWay, UpdateSourceTrigger=LostFocus}"
Minimum="0" Maximum="100" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="30" MinWidth="50" Header="%" Binding="{Binding (clientsideFileManager:CfmModel+FileInformationForOverview.ProgressValue)}" />
<DataGridHyperlinkColumn Width="320" Header="Deeplink" Binding="{Binding (clientsideFileManager:CfmModel+FileInformationForOverview.Deeplink)}">
<DataGridHyperlinkColumn.ElementStyle>
<Style>
<EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
</DataGrid.Columns>
</DataGrid>
在列表中:
public readonly ObservableCollection<CfmModel.FileInformationForOverview> OverviewList =
new ObservableCollection<CfmModel.FileInformationForOverview>();
但这会完全阻塞GUI,直到上传完成
希望你们能帮助我:
编辑:
我的代码更改为以下内容:
数据网格:
<DataGrid Grid.Row="0" x:Name="DataGridOverview" MinHeight="250" MinWidth="705"
ItemsSource="{Binding OverViewList}" Style="{StaticResource DataGridStyle}" >
<DataGrid.Columns>
<DataGridTextColumn Width="*" MinWidth="100" Header="Name" Binding="{Binding Path=(clientsideFileManager:CfmModel+FileInformationForOverview.Name),UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Width="1*" MinWidth="50" Header="Größe" Binding="{Binding Path=(clientsideFileManager:CfmModel+FileInformationForOverview.HumanReadableSize),UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Width="1*" MinWidth="70" Header="Typ" Binding="{Binding Path=(clientsideFileManager:CfmModel+FileInformationForOverview.Type),UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Width="1*" MinWidth="70" Header="Status" Binding="{Binding Path=(clientsideFileManager:CfmModel+FileInformationForOverview.Status),UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTemplateColumn Header="Progress" Width="1*" MinWidth="70">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ProgressBar Name="progressBar"
Value="{Binding Path=(clientsideFileManager:CfmModel+FileInformationForOverview.ProgressValue), Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Minimum="0" Maximum="100" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="30" MinWidth="50" Header="%" Binding="{Binding (clientsideFileManager:CfmModel+FileInformationForOverview.ProgressValue),UpdateSourceTrigger=PropertyChanged}" />
<DataGridHyperlinkColumn Width="320" Header="Deeplink" Binding="{Binding (clientsideFileManager:CfmModel+FileInformationForOverview.Deeplink),UpdateSourceTrigger=PropertyChanged}">
<DataGridHyperlinkColumn.ElementStyle>
<Style>
<EventSetter Event="Hyperlink.Click" Handler="DG_Hyperlink_Click"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
</DataGrid.Columns>
</DataGrid>
以及对象存储在MainWindow.xam.cs中的列表:
public BindingList<CfmModel.FileInformationForOverview> _overviewList
=
new BindingList<CfmModel.FileInformationForOverview>();
DataGridOverview.DataContext = this;
DataGridOverview.ItemsSource = OverviewList;
DataGridOverview.ItemsSource = _overviewList;
public BindingList<CfmModel.FileInformationForOverview> OverviewList
{
get { return _overviewList; }
set { _overviewList = value; }
}
现在没有任何例外,但也没有刷新
我所做的更改是,当一个数据块完成上载时,将FileInformationForOverview.ProgresValue+1计数。尝试实现如下数据类: 这应该是可行的,而且我假设您的实际工作必须在不同的线程中完成:
await Task.Run(()=> Calculate());
这是上课时间
public class FileInformationForOverview : INotifyPropertyChanged
{
public int IdOnList { get; set; }
public String Name { get; set; }
public Int64 Size { get; set; }
public String HumanReadableSize { get; set; }
public String Path { get; set; }
public String Type { get; set; }
public long JobId { get; set; }
public Boolean IsChecked { get; set; }
public cfmData CfmData { get; set; }
public bool Paused { get; set; }
public String Deeplink { get; set; }
public String Status { get; set; }
public String LastStatus { get; set; }
private int _progressValue;
public int ProgressValue
{
get { return _progressValue; }
set
{
_progressValue = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
绑定的更新触发器位于丢失的焦点上?为什么?此外,您的类未实现INotifyPropertyChanged,这对于实际发送ValueChanged事件非常有用,因此接口将更新。我尝试在类上使用INotifyPropertyChanged,但每次我尝试在更新时向列表中添加内容时,都会出现InvalidArgumentException,告诉我键为null。我已经多次证明了这一点,并且绑定到Datagrid的值从来都不是空的。当我试图跟踪异常时,VS告诉我无法找到源代码。您的列表还需要有一个{get;set;}才能成为bindableupdated代码,但DataGrid不会显示ItemsAbservableCollection上的更改。为什么更改为BindingList?。dataContext只能在windows构造函数中设置为this.dataContext=this;而不是数据网格。在XAML中,进度条bindign应该是Value={Binding ProgressValue}我的工作正在另一个线程中完成。它在后台工作,但进度没有显示在网格中。将显示添加到列表中的项目,但进度信息不会随时间而更新。当我尝试实现INotifyPropertyChanged时,它给出了一个IllegalArgumentException,Key不能为Null。。你能解释一下我需要设置什么:DataContext和ItemsSource。如果我理解正确,ItemsSource是我的概览列表,DataContext应该是我的主窗口类,对吗?
public BindingList<CfmModel.FileInformationForOverview> _overviewList
=
new BindingList<CfmModel.FileInformationForOverview>();
DataGridOverview.DataContext = this;
DataGridOverview.ItemsSource = OverviewList;
DataGridOverview.ItemsSource = _overviewList;
public BindingList<CfmModel.FileInformationForOverview> OverviewList
{
get { return _overviewList; }
set { _overviewList = value; }
}
await Task.Run(()=> Calculate());
public class FileInformationForOverview : INotifyPropertyChanged
{
public int IdOnList { get; set; }
public String Name { get; set; }
public Int64 Size { get; set; }
public String HumanReadableSize { get; set; }
public String Path { get; set; }
public String Type { get; set; }
public long JobId { get; set; }
public Boolean IsChecked { get; set; }
public cfmData CfmData { get; set; }
public bool Paused { get; set; }
public String Deeplink { get; set; }
public String Status { get; set; }
public String LastStatus { get; set; }
private int _progressValue;
public int ProgressValue
{
get { return _progressValue; }
set
{
_progressValue = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}