wpf - DataGrid 绑定(bind)在 GroupItem 样式声明中不起作用

标签 wpf xaml binding datagrid groupstyle

我在获取用户控件的资源部分中定义的工作绑定(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/

相关文章:

wpf - 将数据表绑定(bind)到 WPF 数据网格的问题

silverlight - 将图像放在页面顶部而不干扰单击事件 Windows Phone

c# - 在 WPF 应用程序中使用服务定位器模式时的 View 模型范围

wpf - 是否有一组接口(interface)可以实现以避免在 WPF 绑定(bind)中有转换器

wpf - 如何在 Adob​​e Illustrator 或 Expression Design 文件中嵌入 png 以创建 XAML

wpf - 带图像的按钮,为什么单击图像时按钮命令不触发?

c# - 使用 MVVM 模式时,x :Bind to event handlers located in the ViewModel? 是否可以

wpf - 在 DependencyProperty 的 CoerceValueCallback 中取消更新时如何更新绑定(bind)源?

安卓服务: onBind(Intent) and onUnbind(Intent) is called just once

c# - 如果通过反射设置 View 模型属性,则 WPF 绑定(bind)不起作用