Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working from documentation for a biometrics sdk.Importing the clockings file and reading it line by line and setting it to the correct columns in a Winform listview works fine but doing it in WPF has me completely stumped

This is a line from the file.There are no header columns in the file

18 2012-07-25 12:35:15 1 0 1 0

This is my xaml
XML
<ListView Name="lstAttlog" Margin="10,78,10,10" ItemsSource="{Binding Proccess}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="80"
                                    DisplayMemberBinding="{Binding PIN2}"
                                    Header="PIN" />
                    <GridViewColumn Width="120"
                                    DisplayMemberBinding="{Binding Time_second}"
                                    Header="Time" />
                    <GridViewColumn Width="80"
                                    DisplayMemberBinding="{Binding DeviceID}"
                                    Header="Device ID" />
                    <GridViewColumn Width="80"
                                    DisplayMemberBinding="{Binding Status}"
                                    Header="Status" />
                    <GridViewColumn Width="80"
                                    DisplayMemberBinding="{Binding Verify}"
                                    Header="Verify" />
                    <GridViewColumn Width="80"
                                    DisplayMemberBinding="{Binding WorkCode}"
                                    Header="WorkCode" />

                </GridView>
            </ListView.View>
        </ListView>

This is the class called Usb
C#
public class Udisk
    {
       public void GetAttLogFromDat(byte[] DataBuf, int OneLogLength, out string PIN2, out string Time_second, out string DeviceID,
            out string Status, out string Verified, out string Workcode)
        {
            int i;
            int Index = 0;

            PIN2 = "";
            Time_second = "";
            DeviceID = "";
            Status = "";
            Verified = "";
            Workcode = "";

            for (i = Index; i < OneLogLength; i++)
            {
                if (DataBuf[i] == 9)//Ascii code 9 stands for the tab key on your keyboard.
                {
                    byte[] PIN2Buf = new byte[i];
                    Array.Copy(DataBuf, 0, PIN2Buf, 0, i);
                    PIN2 = System.Text.Encoding.Default.GetString(PIN2Buf);
                    Index = i;
                    break;
                }
            }

            for (i = Index + 1; i < OneLogLength; i++)
            {
                if (DataBuf[i] == 9)//the second tab key
                {
                    byte[] TimeBuf = new byte[i - Index - 1];
                    Array.Copy(DataBuf, Index + 1, TimeBuf, 0, i - Index - 1);
                    Time_second = System.Text.Encoding.Default.GetString(TimeBuf);
                    Index = i;
                    break;
                }
            }

            for (i = Index + 1; i < OneLogLength; i++)
            {
                if (DataBuf[i] == 9)//the third tab key
                {
                    byte[] DeviceIDBuf = new byte[i - Index - 1];
                    Array.Copy(DataBuf, Index + 1, DeviceIDBuf, 0, i - Index - 1);
                    DeviceID = System.Text.Encoding.Default.GetString(DeviceIDBuf);
                    Index = i;
                    break;
                }
            }

            for (i = Index + 1; i < OneLogLength; i++)
            {
                if (DataBuf[i] == 9)//the fourth tab key
                {
                    byte[] StatusBuf = new byte[i - Index - 1];
                    Array.Copy(DataBuf, Index + 1, StatusBuf, 0, i - Index - 1);
                    Status = System.Text.Encoding.Default.GetString(StatusBuf);
                    Index = i;
                    break;
                }
            }

            for (i = Index + 1; i < OneLogLength; i++)
            {
                if (DataBuf[i] == 9)//the fifth tab key
                {
                    byte[] VerifiedBuf = new byte[i - Index - 1];
                    Array.Copy(DataBuf, Index + 1, VerifiedBuf, 0, i - Index - 1);
                    Verified = System.Text.Encoding.Default.GetString(VerifiedBuf);
                    Index = i;
                    break;
                }
            }

            for (i = Index + 1; i < OneLogLength; i++)
            {
                if (DataBuf[i] == 13)//the sixth tab key
                {
                    byte[] WorkcodeBuf = new byte[i - Index - 1];
                    Array.Copy(DataBuf, Index + 1, WorkcodeBuf, 0, i - Index - 1);
                    Workcode = System.Text.Encoding.Default.GetString(WorkcodeBuf);
                    break;

                }
            }
        }

This is the class called Proccess
C#
public class Proccess
    {
        public String PIN2 { get; set; }
        public DateTime Time_second { get; set; }
        public string DeviceID { get; set; }
        public string Status { get; set; }
        public string Verify { get; set; }
        public string WorkCode { get; set; }
    }

and this is the eventhandler to read file into listview
C#
Udisk usb = new Udisk();
            byte[] byDataBuf = null;
            int iLength;//length of the bytes to get from the data

            string sPIN2 = "";
            string sVerified = "";
            string sTime_second = "";
            string sDeviceID = "";
            string sStatus = "";
            string sWorkcode = "";


            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "attlog(*.dat)|*.dat";
            ofd.FileName = "attlog.dat";
            Nullable<bool> result = ofd.ShowDialog();
            if (result == true)
            {
               
                FileStream fs = new FileStream(ofd.FileName, FileMode.OpenOrCreate, FileAccess.Read);
                byDataBuf = File.ReadAllBytes(ofd.FileName);
                iLength = Convert.ToInt32(fs.Length);

                lstAttlog.Items.Clear();
                int iStartIndex = 0;
                int iOneLogLength;

                for (int i = iStartIndex; i < iLength; i++)
                {
                    if (byDataBuf[i] == 13 && byDataBuf[i + 1] == 10)
                        {
                            iOneLogLength = (i + 1) + 1 - iStartIndex;
                            byte[] bySSRAttLog = new byte[iOneLogLength];
                            Array.Copy(byDataBuf, iStartIndex, bySSRAttLog, 0, iOneLogLength);
                            usb.GetAttLogFromDat(bySSRAttLog, iOneLogLength, out sPIN2, out sTime_second, out sDeviceID, out sStatus, out sVerified, out sWorkcode);



I am struggeling to figure out how to add a
C#
List<Proccess> process = new List<Proccess>();
and adding the the data to a WPF listview

Much appreciated
Gerhard-Louis
Posted
Comments
Kenneth Haugland 17-Jan-15 3:35am    
Think you should use an observable collection, as it will trigger if items are added to the list. Then its just a matter of binding it to the listview.

I normally do the binding in code behind; xaml:
ItemsSource="{Binding}"
and code:
lstAttlog.ItemsSource = process
Gerhard_Louis 17-Jan-15 10:20am    
Thanks Kenneth
George Swan 17-Jan-15 5:57am    
I think you are doing this the hard way. Try downloading FileHelpers from https://github.com/MarcosMeli/FileHelpers
With FileHelpers.dll you can the read your file straight into a Proccess[] with just a couple of lines of code and it's free.
Gerhard_Louis 17-Jan-15 10:23am    
You are right.Moving from Delphi to C# and WPF is one giant leap and its difficult to get out a set mindset.I will have a look.Thank you
Kenneth Haugland 17-Jan-15 10:27am    
http://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx
is a build in solution that is really simple to use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900