带有DataGrid的TabControl:无法为目标元素找到管理FrameworkElement或FrameworkContentElement

时间:2016-02-20 17:24:39

标签: wpf xaml data-binding

我遇到一个恼人的绑定错误,基本上是我用这个控件的最后一个障碍:

  

System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:路径= SearchResult所;的DataItem = NULL; target元素是'CollectionViewSource'(HashCode = 6017800); target属性为'Source'(类型'Object')

这是TabControl的标记:

<TabControl ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab}">

    <TabControl.ItemTemplate>
        <!-- tab header template works as intended -->
    </TabControl.ItemTemplate>

    <TabControl.ContentTemplate>

        <DataTemplate DataType="controls:SearchResultsViewModel">

            <DataTemplate.Resources>
                <CollectionViewSource x:Key="GroupedSearchResults" 
                                      Source="{Binding SearchResults}">
                    <CollectionViewSource.SortDescriptions>
                        <!-- sort descriptions -->
                    </CollectionViewSource.SortDescriptions>
                    <CollectionViewSource.GroupDescriptions>
                        <!-- group descriptions -->
                    </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource>
            </DataTemplate.Resources>

            <controls:GroupingGrid ShowGroupingItemCount="True"
                                   ItemsSource="{Binding Source={StaticResource GroupedSearchResults}}"
                                   SelectedItem="{Binding SelectedItem}">
                <DataGrid.Columns>
                    <!-- column definitions -->
                </DataGrid.Columns>
            </controls:GroupingGrid>

        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

设计师在任何地方都没有给我任何曲折的线条,这似乎表明一切都很好;我只在运行时获得绑定错误,但是ViewModels(serach结果)完全按预期填充...它们由于某种原因不会绑定到CollectionViewSource

我做错了什么?

2 个答案:

答案 0 :(得分:1)

以下是基于http://www.broculos.net/2014/04/wpf-how-to-bind-collectionviewsource.html的解决方法。它的工作原理是引入StackPanel并将CollectionViewSource移动到StackPanel资源:

<DataTemplate DataType="controls:SearchResultsViewModel">
    <StackPanel>
        <StackPanel.Resources>
            <CollectionViewSource x:Name="ViewSource" x:Key="GroupedSearchResults" Source="{Binding Test}">
                <CollectionViewSource.SortDescriptions>
                    <componentModel:SortDescription PropertyName="QualifiedMemberName.QualifiedModuleName.Name" />
                </CollectionViewSource.SortDescriptions>
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="QualifiedMemberName.QualifiedModuleName.Name" />
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
        </StackPanel.Resources>
        <controls:GroupingGrid ShowGroupingItemCount="True" x:Name="TabGrid"
                               ItemsSource="{Binding Source={StaticResource GroupedSearchResults}}"
                               SelectedItem="{Binding SelectedItem}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=SearchResults_ModuleName}" Binding="{Binding QualifiedMemberName.QualifiedModuleName}" />
                <DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=SearchResults_MemberName}" Binding="{Binding QualifiedMemberName.MemberName}" />
                <DataGridTextColumn Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=SearchResults_Location}" Binding="{Binding Selection}" Width="*" />
            </DataGrid.Columns>
        </controls:GroupingGrid>
    </StackPanel>
</DataTemplate>

答案 1 :(得分:0)

不知道为什么全XAML解决方案无法运行。我放弃并通过在ViewModel上公开CollectionViewSource成员来实现它:

public SearchResultsViewModel(string header, IEnumerable<SearchResultItem> searchResults)
{
    _header = header;
    _searchResults = new ObservableCollection<SearchResultItem>(searchResults);
    _searchResultsSource = new CollectionViewSource();
    _searchResultsSource.Source = _searchResults;
    _searchResultsSource.GroupDescriptions.Add(new PropertyGroupDescription("QualifiedMemberName.QualifiedModuleName.Name"));
    _searchResultsSource.SortDescriptions.Add(new SortDescription("QualifiedMemberName.QualifiedModuleName.Name", ListSortDirection.Ascending));
    _closeCommand = new DelegateCommand(ExecuteCloseCommand);
}

// not really needed anymore
private readonly ObservableCollection<SearchResultItem> _searchResults;
public ObservableCollection<SearchResultItem> SearchResults { get { return _searchResults; } }

private readonly CollectionViewSource _searchResultsSource;
public CollectionViewSource SearchResultsSource { get { return _searchResultsSource; } }

然后在XAML中,我将GroupingGrid的{​​{1}}绑定更改为新ItemsSource属性的View成员,如下所示:

SearchResultsSource

最后让我的网格填充:

Search Results tabbed toolwindow working as expected