我在获取用户控件的资源部分中定义的工作绑定(bind)时遇到了一些麻烦。当我将它绑定(bind)到数据网格的列时,相同的绑定(bind)似乎稍后在 xaml 中起作用。在样式声明中它只是不会显示数据。
我得到的错误是
System.Windows.Data Error: 40 : BindingExpression path error: 'ReceivedDate' property not found on 'object' ''CollectionViewGroupInternal' (HashCode=5477078)'. BindingExpression:Path=ReceivedDate; DataItem='CollectionViewGroupInternal' (HashCode=5477078); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
以下绑定(bind) 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
}
我尝试将绑定(bind)的路径更改为/ReceivedDate。
让我感到困惑的是,相同的绑定(bind)在其他地方声明时有效。例如在各个列标题中。
最佳答案
Expander.Header
没有得到您的 View 模型之一。相反, header 获取一个继承自 CollectionViewGroup
的对象。有两个名为 Name
的属性和 ItemCount
.
关于wpf - DataGrid 绑定(bind)在 GroupItem 样式声明中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30407822/