将ObservableCollection的对象绑定到DataGrid c#.net

时间:2015-01-19 13:18:37

标签: c# .net wpf

我在WPF中遇到了问题。

我有一个DataGrid,如:

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>

与DataGrid绑定的类是:

       public class FileInformationForOverview
    {
        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 int ProgressValue { 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; }
    }

在列表中:

        public readonly ObservableCollection<CfmModel.FileInformationForOverview> OverviewList =
        new ObservableCollection<CfmModel.FileInformationForOverview>();

列表在我的MainWindow.xaml.cs中,类FileInformationForOverview在另一个类(CFMModel.cs)

它是一个上传数据的软件。用户应该看到DataGrid中每个文件的进度。

我的问题是,我想在每次更改时更新Progress the Update。

当我在Web上阅读时,DataGrid列应该在每次绑定更改时更新。我做错了吗?

到目前为止,我得到了那样的刷新:

        public void model_OnProgressUpdate()
    {
        Dispatcher.BeginInvoke(new Action (()=>DataGridOverview.Items.Refresh()));
        Dispatcher.BeginInvoke(new Action(() => DataGridOverview.Focus()));
    }

但这会完全阻止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>

具有要在DataGrid中显示的对象的类(在类CFMModel.cs中):

    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("ProgressValue");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;


        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

对象存储的列表(在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; }
    }

现在没有任何例外,但也没有刷新。

所有我改变的是,当Chunk完成上传时,计算FileInformationForOverview.ProgresValue +1。

1 个答案:

答案 0 :(得分:0)

尝试实现这样的数据类: 这应该有用,我还假设你的实际工作必须在另一个线程中完成:

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));
        }
    }