CollectionView与扩展器没有找到属性,虽然工作在扩展器头
本文关键字:扩展器 工作 CollectionView 属性 | 更新日期: 2023-09-27 18:15:35
我正在做一个c# WPF项目,我正在尝试使用CollectionView将元素分组在一起。
数据通过存储在数组中的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 = 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);
}
下面是CallLogInformation类
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 string callType { get; set; }
public long date { get; set; }
public int callDuration { get; set; }
public CallLogInformation()
{
//callType = CallType.UNKNOWN;
}
}
下面是如何将数组添加到集合视图
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);
}
我正在尝试按callType属性分组,因此呼叫分组为传入/传出/未接
当我把扩展器部分的代码在它说,它找不到属性callType,但如果我删除扩展器,只是添加callType到列表组没有扩展器它的工作,所以callType绝对是工作的,所以我不明白为什么当扩展器使用它有一个问题。
下面是WPF XAML<ListView Height="397" HorizontalAlignment="Left" Margin="491,29,0,0" Name="lstCallLogInformation"
VerticalAlignment="Top" Width="320">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding callType}" FontWeight="Bold" Foreground="Gray" VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
<GroupStyle />
</ListView.GroupStyle>
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding contactNameOrPhoneNumber}" FontWeight="Bold" />
<TextBlock Text="{Binding callType}" />
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
当您使用groupdescription(视图对象)定义组时(一个CollectionViewSource对象或派生的对象CollectionView)将每个组包装在一个CollectionViewGroup对象中。
因此,当您自定义组样式时,您正在处理CollectionViewGroup对象。您仍然可以访问原始数据通过CollectionViewGroup的Items属性键入。"Name"是CollectionViewGroup Object的一个属性。
原文:http://social.msdn.microsoft.com/Forums/vstudio/en-US/731a3230-65db-497e-afbb-6bb9f8b378d5/listview-grouping-with-expander?forum=wpf
可以试试{Binding Name}
而不是{Binding callType}
。正如注释所说,"Name"应该指的是CollectionViewGroup。名称,它最终应该映射到callType。
如果你需要更多的信息,你已经给了CollectionViewGroup.Items
,其中应该有所有必要的项目为特定组。