WPF datagrid 實(shí)現(xiàn)加載量級數(shù)據(jù)不卡頓(待修改)

通知基類

    public class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected void UpdateProper<T>(ref T properValue, T newValue, [CallerMemberName] string properName = "",
            [CallerLineNumber] int line = 0, [CallerFilePath] string file = "")
        {
            if (object.Equals(properValue, newValue))
                return;

            properValue = newValue;
            RaisePropertyChanged(properName);
        }

        protected void UpdateProper<T>(ref T properValue, T newValue,string properName)
        {
            if (object.Equals(properValue, newValue))
                return;

            properValue = newValue;
            RaisePropertyChanged(properName);
        }

2、數(shù)據(jù)

namespace ChassisByWireSystem.ViewModels
{
    public class CandataInfo : Common.NotifyBase
    {
        uint ordernumber;
        public uint 序號
        {
            get => ordernumber;
            set { UpdateProper(ref ordernumber, value); }
        }

        string direction;
        public string 方向
        {
            get => direction;
            set { UpdateProper(ref direction, value); }
        }

        string time;
        public string 時(shí)間
        {
            get => time;
            set { UpdateProper(ref time, value); }
        }

        string frameid;
        public string 幀ID
        {
            get => frameid;
            set { UpdateProper(ref frameid, value); }
        }

        string frameformat;
        public string 幀格式
        {
            get => frameformat;
            set { UpdateProper(ref frameformat, value); }
        }

        string frametype;
        public string 幀類型
        {
            get => frametype;
            set { UpdateProper(ref frametype, value); }
        }

        string dlc;
        public string DLC
        {
            get => dlc;
            set { UpdateProper(ref dlc, value); }
        }

        string candata;
        public string 數(shù)據(jù)
        {
            get => candata;
            set { UpdateProper(ref candata, value); }
        }

        public CandataInfo() { }
        public CandataInfo(uint number, string dir, string time, string fid, string fformat, string ftype, string datalenth, string candata)
        {
            序號 = number;
            方向 = dir;
            時(shí)間 = time;
            幀ID = fid;
            幀格式 = fformat;
            幀類型 = ftype;
            DLC = datalenth;
            數(shù)據(jù) = candata;
        }

    }

    public class CanData : System.Collections.ObjectModel.ObservableCollection<CandataInfo> { }
}

3、XAML文件引用

    <Window.Resources>
        <viewmodels:CanData x:Key="DataSend">

        </viewmodels:CanData>
        <viewmodels:CanData x:Key="DataRecv">

        </viewmodels:CanData>
    </Window.Resources>
    <DataGrid x:Name="datagrid1" EnableRowVirtualization="True" FontSize="12" EnableColumnVirtualization="False" 
        ItemsSource="{Binding Source={StaticResource DataSend}}" IsReadOnly="True">
    </DataGrid>
    <DataGrid Grid.Column="1" x:Name="datagrid2" EnableRowVirtualization="True" FontSize="12" EnableColumnVirtualization="False" 
        ItemsSource="{Binding Source={StaticResource DataRecv}}" IsReadOnly="True">
    </DataGrid>

4、代碼樣例

new Thread(o =>
{
    while (true)
    {
        Dispatcher.Invoke(() =>
        {
            if (frameDir.Equals("發(fā)送"))
            {
                CanData datasend = (CanData)this.FindResource("DataSend");
                datasend.Add(new CandataInfo((uint)datagrid1.Items.Count, frameDir, time,
                    sframeid, framformat, frametype, frameDlc, sdata));
                datagrid1.ScrollIntoView(datagrid1.Items[datagrid1.Items.Count - 1]);
            }
            else
            {
                CanData dataRecv = (CanData)this.FindResource("DataRecv");
                dataRecv.Add(new CandataInfo((uint)datagrid2.Items.Count, frameDir, time,
                    sframeid, framformat, frametype, frameDlc, sdata));
                datagrid2.ScrollIntoView(datagrid2.Items[datagrid2.Items.Count - 1]);

            }
        });
        Thread.sleep(1);
    }
})
{ IsBackground = true }.Start();

5、


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【摘要】 輕量級數(shù)據(jù)處理腳本是桌面數(shù)據(jù)分析師的必備工具,本文從此類工具中精心挑選了四種,從易用性、開發(fā)效率、數(shù)據(jù)源...
    西柚學(xué)報(bào)表閱讀 862評論 0 4
  • 一、樣式(Style)與模板(Template)二、資源字典ResourceDictionary三、數(shù)據(jù)綁定四、特...
    李姍姍_8ef1閱讀 696評論 0 0
  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友。感恩相遇!感恩不離不棄。 中午開了第一次的黨會,身份的轉(zhuǎn)變要...
    余生動聽閱讀 10,835評論 0 11
  • 彩排完,天已黑
    劉凱書法閱讀 4,470評論 1 3
  • 沒事就多看看書,因?yàn)楦褂性姇鴼庾匀A,讀書萬卷始通神。沒事就多出去旅游,別因?yàn)闆]錢而找借口,因?yàn)橹灰闶〕詢€用,來...
    向陽之心閱讀 4,973評論 3 11

友情鏈接更多精彩內(nèi)容