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

Red*_*irt 1 wpf xaml binding datagrid groupstyle

我无法在用户控件的资源部分中定义的绑定中正常工作。稍后,当我将它绑定到datagrid的列时,相同的绑定似乎可以在xaml中使用。在样式声明中时,它只是不显示数据。

我得到的错误是

System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“ CollectionViewGroupInternal”(HashCode = 5477078)上找不到“ ReceivedDate”属性。BindingExpression:Path = ReceivedDate; DataItem ='CollectionViewGroupInternal'(HashCode = 5477078); 目标元素是'TextBlock'(Name =''); 目标属性为“文本”(类型为“字符串”)

下面的绑定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>
Run Code Online (Sandbox Code Playgroud)

在此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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

CurrentMailBoxContent是一个

ObservableCollection<MailMessage>;
Run Code Online (Sandbox Code Playgroud)

和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
}
Run Code Online (Sandbox Code Playgroud)

我试过将绑定的路径更改为/ ReceivedDate。

令我困惑的是,当在其他地方声明时,相同的绑定有效。例如在各种列标题中。

小智 5

Expander.Header没有得到您的视图模型之一。相反,标头会获得一个继承自CollectionViewGroup该对象的对象,该对象具有两个名为Name和的属性ItemCount