BindingExpression недопустимо для ListCollectionView в CollectionViewSource - PullRequest
0 голосов
/ 13 ноября 2018

Мне нужно, чтобы коллекция отображалась в ListView. Для этого у меня есть CompositeCollection с одним ListViewItem в качестве панели поиска для коллекции. Я положил его вместе со списком "n" предметов. Причина, по которой это делается, заключается в том, что на поисковую панель не влияет сортировка Проблема в том, что CollectionView, который я связываю с CollectionViewSource, производит BindingError, и я не знаю почему.

Мой XAML-код для коллекций:

<Grid.Resources>
      <CollectionViewSource x:Key="ElementSource" Source="{Binding ElementsCollectionView}"/>
      <CompositeCollection x:Key="CompositeCollection">
           <ListViewItem Content="{Binding HeaderRow}"/>
           <CollectionContainer Collection="{Binding Source={StaticResource ElementSource}}"/>
      </CompositeCollection>
</Grid.Resources>

И коллекции, которые я использую

private NotifyCollection<DataEntry> _data;
public NotifyCollection<DataEntry> Data
{
    get { return this._data; }
    set { this.SetProperty(ref this._data, value); }
}

private CollectionView _elementsCollectionView;
public CollectionView ElementsCollectionView
{
    get { return this._elementsCollectionView; }
    set
    {
        if (value == null) return;
        this._elementsCollectionView = value;
    }
}

private CompositeCollection _tableItems;
public CompositeCollection TableItems
{
    get { return this._tableItems; }
    set { this.SetProperty(ref this._tableItems, value); }
}

private DataEntry _headerRow;
public DataEntry HeaderRow
{
    get { return this._headerRow; }
    set { this.SetProperty(ref this._headerRow, value); }
}

И Инициализируемые Коллекции:

public void SetCollection()
{
    this.TableItems.Add(this.HeaderRow);
    this.TableItems.Add(this.ElementsCollectionView);
}

this.TableViewModel.ElementsCollectionView =  
     (CollectionView)CollectionViewSource.GetDefaultView(this.Data);

Как видите, все в порядке. NotifyCollection наследуется от ObservableCollection и должен выполнять свою работу. Если я добавляю HeaderRow в «ElementsCollectionView», все работает нормально, за исключением того, что HeaderRow отсортирован. Если я добавлю CollectionView к CollectionViewSource, я получу BindingError:

 System.Windows.Data Error: 5 : Value produced by BindingExpression is      
 not valid for target property.; 
 Value='System.Windows.Data.ListCollectionView' 
 BindingExpression:Path=ElementsCollectionView; 
 DataItem='AuftragsverwaltungDataTableViewModel' (HashCode=57170378); 
 target element is 'CollectionViewSource' (HashCode=52642430); target property is 'Source' (type 'Object')

1 Ответ

0 голосов
/ 13 ноября 2018

Нельзя установить для свойства Source для CollectionViewSource значение ListCollectionView. Это бросит ArgumentException. Сортируйте коллекцию NotifyCollection<DataEntry> и связывайте ее с ней.

...