我在使用我的用户控件的resouces部分中定义的绑定工作时遇到了一些麻烦。当我将它绑定到datagrid的列时,相同的绑定似乎稍后在xaml中工作。在样式声明中它只是不显示数据。
我得到的错误是
System.Windows.Data错误:40:BindingExpression路径错误: 'object'上找不到'ReceivedDate'属性 ''CollectionViewGroupInternal'(HashCode = 5477078)'。 BindingExpression:路径= ReceivedDate; DataItem ='CollectionViewGroupInternal'(HashCode = 5477078);目标 element是'TextBlock'(Name =''); target属性是'Text'(类型 '字符串')
以下绑定ReceivedDate未在运行时解析。
<UserControl.Resources>
<!-- Grouped Items Header: Show the messages in a group. ex: date received -->
<Style x:Key="GroupedItemsHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander x:Name="exp" IsExpanded="True"
Background="LightGray"
Foreground="Black">
<Expander.Header>
<TextBlock Text="{Binding Path=ReceivedDate, Converter={StaticResource DateToSortGroupConverter}}" Foreground="Black"/>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
在这个UserControl的代码隐藏中,我按如下方式设置itemsList。
void MailController_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "CurrentMailBoxContent")
{
var currentMailBox = ((App) Application.Current).MailController.CurrentMailBoxContent;
var collection = new ListCollectionView(currentMailBox);
collection.GroupDescriptions.Add(new PropertyGroupDescription("ReceivedDate"));
ContentDataGrid.ItemsSource = collection;
}
}
CurrentMailBoxContent是
ObservableCollection<MailMessage>;
和ReceivedDate是MailMessage类中的属性。
public class MailMessage : INotifyPropertyChanged
{
#region Fields
public event PropertyChangedEventHandler PropertyChanged;
private DateTime _receivedDate;
#endregion
#region Constructor
public MailMessage(){}
#endregion
#region Properties
public DateTime ReceivedDate
{
get { return _receivedDate; }
set
{
if (_receivedDate == value) return;
_receivedDate = value;
OnPropertyChanged("ReceivedDate");
}
}
#endregion
#region methods
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
我已尝试将绑定路径更改为/ ReceivedDate。
让我感到困惑的是,当在其他地方宣布时,同样的绑定会起作用。比如在各个列标题中。
答案 0 :(得分:3)
Expander.Header
没有获得您的一个视图模型。而是标头获取一个继承自CollectionViewGroup
的对象,该对象具有两个名为Name
和ItemCount
的属性。