C# 使用CollectionView对列表视图中的项目进行分组

C# 使用CollectionView对列表视图中的项目进行分组,c#,wpf,C#,Wpf,我正在开发一个C应用程序,在这个应用程序中,我希望显示读取到程序中的调用日志信息。我希望将项目添加到组中的列表视图中。组应基于数组中的调用类型,例如入站/出站/错过 下面是我如何创建从XML消息读入数据的数组 public void processCallLogInfo(string xml) { List<CallLogInformation> callLogInformationList = new List<CallLogInfor

我正在开发一个C应用程序,在这个应用程序中,我希望显示读取到程序中的调用日志信息。我希望将项目添加到组中的列表视图中。组应基于数组中的调用类型,例如入站/出站/错过

下面是我如何创建从XML消息读入数据的数组

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);
        }
当我运行程序时,我在控制台中得到以下错误

System.Windows.Data错误:40:BindingExpression路径错误:在“object”CallLogiInformation“HashCode=24457251”上找不到“callType”属性。空的


将callType更改为属性而不是字段

乙二醇

WPF查找属性而不是字段,在您当前的CallLogiInformation类实现中,callType被定义为field

您可能需要将其他属性绑定到UI,以便需要将它们全部转换

范例

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;}
}

您的CallLogInformation类没有属性,只有公共字段,当然还有枚举声明。但是,数据绑定仅适用于公共属性。您应该阅读MSDN上的文章。最好将所有公共字段更改为属性。现在很明显,谢谢,我完全忘记了这一点
<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>
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;
    }
public CallType callType { get; set;}
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;}
}