按下按钮对DataGrid值进行排序

时间:2017-08-04 10:27:22

标签: c# wpf datagrid

我正在制作一个下载管理器,而且我在更新进度条列的值时遇到问题,我正在尝试这样做,以便当我按下"下载"按钮只有值低于100%的进度条将显示在DataGird

Program image for reference

Xaml DataGrid代码

            <!--List of the programs currently used/downloaded-->
            <DataGrid x:Name="DG_List" AutoGenerateColumns="False" 
                      IsReadOnly="true" ColumnWidth="*">

                <DataGrid.Columns>

                    <!--#region Program Number -->
                    <DataGridTemplateColumn SortMemberPath="ProgramID" 
                                            Header="#">

                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <!--#region Adds a dot at the end of a number-->
                                <TextBlock>
                                    <TextBlock.Text>
                                        <Binding Path="ProgramID">
                                            <Binding.StringFormat>

                                                {0}.

                                            </Binding.StringFormat>
                                        </Binding>
                                    </TextBlock.Text>
                                </TextBlock>
                                <!--#endregion-->

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>

                    </DataGridTemplateColumn>
                    <!--#endregion-->


                    <!--#region Program Number -->
                    <DataGridTemplateColumn SortMemberPath="Name" 
                                            Header="Name">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <TextBlock Text="{Binding Name}"/>

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->


                    <!--#region Program Number -->
                    <DataGridTemplateColumn SortMemberPath="Size" 
                                            Header="Size">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <TextBlock Text="{Binding Size}"/>

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->


                    <!--#region Progress bar columns -->
                    <DataGridTemplateColumn SortMemberPath="Progress" 
                                            Header="Progress">
                        <DataGridTemplateColumn.CellTemplate>

                            <DataTemplate>

                                <ProgressBar Value="{Binding Progress}" />

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->


                    <!--#region Progress bar columns -->
                    <DataGridTemplateColumn SortMemberPath="Bandwidth" 
                                            Header="Bandwidth">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>

                                <TextBlock Text="{Binding BandWidth}" />

                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <!--#endregion-->

                </DataGrid.Columns>
            </DataGrid>
        </Grid>

活动背后的代码

        private void Btn_Downloading_Click(object sender, RoutedEventArgs e)
        {

            Programs P = new Programs();

            //Loops through every "Program" in the program list
            for (int a = 0; a < P.ProgramList().Count; a++)
            {

                if (P.Progress <= 100)
                {

                    *Here is where the event/Sort supposed to happen*

                }

            }
        }

如果有任何遗漏,请告诉我,我很乐意添加

编辑: 新代码

    public partial class MainWindow : Window
    {

        Programs P = new Programs();

        private bool _showOnlyNotFinishedDownloads = false;


        public MainWindow()
        {

            InitializeComponent();

            //Sets the item source to the list of programs
            //DG_List.ItemsSource = P.ProgramList();

            P.ProgramsView = new CollectionViewSource { Source = P }.View; P.ProgramsView.Filter = FilterPrograms;

        }

但现在P.ProgramsView = new CollectionViewSource { Source = P }.View; P.ProgramsView.Filter = FilterPrograms;会抛出一个异常

&#34; &#39; DownloadManager_v2._5_1.Downloads.Programs&#39;不是财产的有效价值&#39;来源&#39;。&#39; &#34;

1 个答案:

答案 0 :(得分:0)

我建议您使用ICollectionFilter

这可能看起来像这样

在您的viewmodel中添加ICollectionView实例,如下所示

public ICollectionView ProgramsView { get; set; }

和这样的属性

private bool _showOnlyNotFinishedDownloads = false;

在您定义集合后,ItemsSource的{​​{1}}定义DataGrid就像这样(假设ICollectionView是集合的名称。

ProgramsView = new CollectionViewSource {Source = Programs} .View;             ProgramsView.Filter = FilterPrograms;

像这样添加过滤器方法(当{0}}显示时,此方法将返回Programs,否则显示true

Program

当您按下载按钮时,将其添加到将被称为

的方法中
false

并将private bool FilterPrograms(object programObject) { if(_showOnlyNotFinishedDownloads) { Programs program = programObject as Programs; bool showProgram = program.Progress < 100; return showProgram; } else { return true; } } 的{​​{1}}更改为此

_showOnlyNotFinishedDownloads = true;
ProgramsView.Refresh();

当您想要显示所有程序时,您必须设置ItemsSource并致电DataGrid