简体   繁体   中英

Using a CollectionView to group items within a list view

I am working on an C# application where I want to display call log information that is read into the program. I want the items to be added to the list view in a group. The group should be based on the call type in the array, for example, inbound/outbound/missed.

Below is how I am creating the array (the data is read in from an XML message.

public void processCallLogInfo(string xml)
        {
            List<CallLogInformation> callLogInformationList = new List<CallLogInformation>();
            string phoneNumber = null;
            string contactName = null;
            string contactPhoto = null;

            XDocument doc = XDocument.Parse(xml);
            var callLogInformationRoot = doc.Descendants("CallLogInformation");
            foreach (var callLogInformation in callLogInformationRoot)
            {
                var callLogRoot = callLogInformation.Descendants("CallLog");
                foreach (var log in callLogRoot)
                {
                    var logRoot = log.Descendants("LogInformation");
                    foreach (var info in logRoot)
                    {
                        CallLogInformation callLogInfo = new CallLogInformation();
                        callLogInfo.contactInformation = new ContactInformation();
                        phoneNumber = info.Element("PhoneNumber").Value;

                        if (info.Elements("ContactPhoto").Any())
                        {
                            callLogInfo.contactInformation.photoBase64String = info.Element("ContactPhoto").Value;
                        }
                        if (info.Elements("ContactName").Any())
                        {
                            callLogInfo.contactInformation.contactName = info.Element("ContactName").Value;
                            callLogInfo.contactNameOrPhoneNumber = info.Element("ContactName").Value;
                        }
                        else
                        {
                            callLogInfo.contactNameOrPhoneNumber = phoneNumber;
                        }

                        callLogInfo.callType = (CallLogInformation.CallType)Enum.Parse(typeof(CallLogInformation.CallType), info.Element("CallType").Value);
                        callLogInfo.date = long.Parse(info.Element("Date").Value);
                        callLogInfo.callDuration = Int32.Parse(info.Element("Duration").Value);
                        callLogInfo.contactInformation.phoneNumber = phoneNumber;

                        callLogInformationList.Add(callLogInfo);
                        //iCallLogManager.addCallLogItemToGUI(callLogInfo);
                    }
                }
            }
            iCallLogManager.addArrayToGui(callLogInformationList);
        }

Below is where I am trying to load the collection and group by the call type, eg INBOUND

public void addArrayToGui(List<CallLogInformation> callLogInformationList)
        {
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(delegate
                {
                    ICollectionView callLogView = CollectionViewSource.GetDefaultView(callLogInformationList);
                    callLogView.GroupDescriptions.Add(new PropertyGroupDescription("callType"));
                    lstCallLogInformation.ItemsSource = callLogView;
                    return null;
                }), null);
        }

Below is the WPF for the ListView

<ListBox Height="397" HorizontalAlignment="Left" Margin="491,29,0,0" Name="lstCallLogInformation" 
                 VerticalAlignment="Top" Width="320">
            <ListBox.GroupStyle>
                <GroupStyle />
            </ListBox.GroupStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding contactNameOrPhoneNumber}" /> 
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

The CallLogInformation class is defined as follows

public class CallLogInformation
    {
        public enum CallType { INCOMING, OUTGOING, MISSED, UNKNOWN };
        public string contactNameOrPhoneNumber;
        public ContactInformation contactInformation;
        public CallType callType = CallType.UNKNOWN;
        public long date = 0;
        public int callDuration = 0;
    }

When I run the program, I get the following error in the console

System.Windows.Data Error: 40 : BindingExpression path error: 'callType' property not found on 'object' ''CallLogInformation' (HashCode=24457251)'. null

change the callType to a property instead of a field

eg

public CallType callType { get; set;}

WPF look up for properties instead of fields, in your current implementation of CallLogInformation class callType is defined as field

you may perhaps need other properties to be binded to the UI so you may need to convert them all

example

public class CallLogInformation
{
    public enum CallType { INCOMING, OUTGOING, MISSED, UNKNOWN };
    public string contactNameOrPhoneNumber { get; set;}
    public ContactInformation contactInformation { get; set;}
    public CallType callType { get; set;}
    public long date { get; set;}
    public int callDuration { get; set;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM