简体   繁体   English

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

[英]DataGrid binding not working in GroupItem style declaration

I'm having some trouble getting a binding to work that is defined in the resouces section of my user control. 我无法在用户控件的资源部分中定义的绑定中正常工作。 The same binding seems to work later on in the xaml when I bind it to a column of the datagrid. 稍后,当我将它绑定到datagrid的列时,相同的绑定似乎可以在xaml中使用。 It just won't display data when in the style declaration. 在样式声明中时,它只是不显示数据。

The error I get is 我得到的错误是

System.Windows.Data Error: 40 : BindingExpression path error: 'ReceivedDate' property not found on 'object' ''CollectionViewGroupInternal' (HashCode=5477078)'. System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“ CollectionViewGroupInternal”(HashCode = 5477078)上找不到“ ReceivedDate”属性。 BindingExpression:Path=ReceivedDate; BindingExpression:路径= ReceivedDate; DataItem='CollectionViewGroupInternal' (HashCode=5477078); DataItem ='CollectionViewGroupInternal'(HashCode = 5477078); target element is 'TextBlock' (Name=''); 目标元素是'TextBlock'(Name =''); target property is 'Text' (type 'String') 目标属性为“文本”(类型为“字符串”)

The below binding ReceivedDate is not resolving at runtime. 下面的绑定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>

In the code-behind for this UserControl I'm setting the itemsList as follows. 在此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 is an CurrentMailBoxContent是一个

ObservableCollection<MailMessage>;

and ReceivedDate is a property in the MailMessage class. 和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
}

I've tried changing the path of the binding to /ReceivedDate. 我试过将绑定的路径更改为/ ReceivedDate。

The thing that confuses me is that the same binding works when declared elsewhere. 令我困惑的是,当在其他地方声明时,相同的绑定有效。 Such as in the various column headers. 例如在各种列标题中。

The Expander.Header does not get one of your view models. Expander.Header无法获得您的视图模型之一。 Instead the header gets an object that inherits from CollectionViewGroup that has two properties named Name and ItemCount . 相反,标头获取一个从CollectionViewGroup继承的对象,该对象具有两个名为NameItemCount属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM