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

时间:2014-07-28 14:28:37

标签: c# wpf

我正在开发一个C#应用程序,我想显示读入程序的调用日志信息。我希望将项目添加到组中的列表视图中。该组应基于数组中的调用类型,例如,inbound / outbound / missed。

以下是我创建数组的方式(从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);
        }

以下是我尝试按呼叫类型加载集合和分组的位置,例如入境

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

以下是ListView的WPF

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

CallLogInformation类定义如下

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

当我运行程序时,我在控制台中收到以下错误

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

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

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

例如

public CallType callType { get; set;}

WPF查找属性而不是字段,在您当前的CallLogInformationcallType实现中定义为字段

您可能需要将其他属性绑定到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;}
}