DataGrid 绑定在 GroupItem 样式声明中不起作用

DataGrid binding not working in GroupItem style declaration

我在获取用户控件资源部分中定义的工作绑定时遇到了一些问题。当我将它绑定到数据网格的列时,相同的绑定似乎稍后在 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')

下面的绑定 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 的 code-behind 中,我将 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 class 中的一个 属性。

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。

让我感到困惑的是,在别处声明时,相同的绑定仍然有效。比如在各个栏目headers.

Expander.Header 没有获得您的视图模型之一。相反,header 得到一个继承自 CollectionViewGroup 的 object,它有两个名为 NameItemCount.

的属性