码迷,mamicode.com
首页 > 其他好文 > 详细

DataGrid绑定DataTable出错

时间:2018-08-16 19:55:27      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:mat   source   rop   text   get   ring   als   ast   string   

直接用DataGrid.ItemSource = DataTable.DefaultView时会出现以下错误:

target element is ‘TextBlock‘ (Name=‘‘); target property is ‘Text

 

需要用CollectionViewSource来做中间转换 .另外实时更新数据,所以不能把它做成StaticResource。

<Grid Margin="5">
        <TabControl>
            <TabItem Name="TabInfo" Header="信息" IsSelected="True" />
            <TabItem Name="TabPlot" Header="画图" IsSelected="True" />
            <TabItem Name="TabData" Header="数据" IsSelected="True" >
                <Grid Margin="5">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <toolkit:DataGrid VirtualizingStackPanel.VirtualizationMode="Recycling" 
                                      VirtualizingStackPanel.IsVirtualizing="True" 
                                      EnableColumnVirtualization="True"
                                      EnableRowVirtualization="True"
                                      RowHeight="23"
                                      ScrollViewer.HorizontalScrollBarVisibility="Visible"
                                      ScrollViewer.VerticalScrollBarVisibility="Visible"
                                      Name="DgdData" Grid.Row="0" AutoGenerateColumns="True" DataContext="{Binding DataTableSource}" ItemsSource="{Binding}" />
                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBox Grid.Column="0" Name="TxtPageSize" Width="60" Height="26" Margin="0 10 0 10" Text="{Binding DataPageSize}" />
                        <TextBox Grid.Column="0" Name="TxtPageNum" Width="60" Height="26" Margin="0 10 0 10" Text="{Binding DataPageNum}" />
                        <Button Grid.Column="2" Name="BtnFirstPage" Content="第一页"  Width="60" Height="26" Margin="10 0 0 0" Click="BtnFirstPage_Click" />
                        <Button Grid.Column="3" Name="BtnPrevPage" Content="上一页" Width="60" Height="30" Margin="10 0 0 0" Click="BtnPrevPage_Click" />
                        <Button Grid.Column="4" Name="BtnNextPage" Content="下一页" Width="60" Height="30" Margin="10 0 0 0" Click="BtnNextPage_Click" />
                        <Button Grid.Column="5" Name="BtnLastPage"  Content="最后一页" Width="60" Height="30" Margin="10 0 0 0" Click="BtnLastPage_Click" />
                    </Grid>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
public void ShowData(VmPlotData data)
        {
            string title;
            this.mData = data;
            this.DataContext = data;

            this.mDtData = new DataTable();
            title = "Time (" + (this.mData.TimeUnit == string.Empty ? "-" : this.mData.TimeUnit) + ")";
            this.mDtData.Columns.Add(new DataColumn(title, typeof(double)));


            foreach (VmDataRequest req in this.mData.Requests)
            {
                foreach (VmDataComponent cmp in req.Components)
                {
                    title = req.Name + ":" + cmp.Name + "(" + (cmp.Unit == string.Empty ? "-" : cmp.Unit) + ")";
                    this.mDtData.Columns.Add(new DataColumn(title, typeof(double)));
                }
            }

            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);

            this.BtnFirstPage.IsEnabled = true;
            this.BtnLastPage.IsEnabled = true;
            this.BtnPrevPage.IsEnabled = true;
            this.BtnNextPage.IsEnabled = true;
        }

        private void BtnFirstPage_Click(object sender, RoutedEventArgs e)
        {
            this.mData.DataPageNum = 1;
            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);
        }

        private void BtnPrevPage_Click(object sender, RoutedEventArgs e)
        {
            this.mData.DataPageNum--;
            this.mData.DataPageNum = this.mData.DataPageNum > 1 ? this.mData.DataPageNum : 1;
            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);
        }

        private void BtnNextPage_Click(object sender, RoutedEventArgs e)
        {
            int maxPageNum = (int)Math.Ceiling((double)this.mData.Times.Count / (double)this.mData.DataPageSize);
            this.mData.DataPageNum++;
            this.mData.DataPageNum = this.mData.DataPageNum > maxPageNum ? maxPageNum : this.mData.DataPageNum;
            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);
        }

        private void BtnLastPage_Click(object sender, RoutedEventArgs e)
        {
            int maxPageNum = (int)Math.Ceiling((double)this.mData.Times.Count / (double)this.mData.DataPageSize);
            this.mData.DataPageNum = maxPageNum;
            this.SetDisData(this.mData.DataPageNum, this.mData.DataPageSize);
        }
        
        private void SetDisData(int pageNum, int pageSize)
        {
            DataRow row;
            int startRow = (pageNum - 1) * pageSize;
            int endRow = startRow + pageSize;
            endRow = this.mData.Times.Count > endRow ? endRow : this.mData.Times.Count;
            this.mDtData.Rows.Clear();


            foreach (VmDataRequest req in this.mData.Requests)
            {
                for (int i = startRow; i < endRow; i++)
                {
                    row = this.mDtData.NewRow();
                    row[0] = this.mData.Times[i];

                    for (int j = 0; j < req.Components.Count; j++)
                    {
                        row[j+1] = req.Components[j].Data[i];
                    }

                    this.mDtData.Rows.Add(row);
                }
            }

            CollectionViewSource itemCollectionViewSource2 = new CollectionViewSource();
            itemCollectionViewSource2.Source = this.mDtData;
            DgdData.DataContext = itemCollectionViewSource2;

        }

 

DataGrid绑定DataTable出错

标签:mat   source   rop   text   get   ring   als   ast   string   

原文地址:https://www.cnblogs.com/xpvincent/p/9488855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!