J'essaie d'utiliser un CollectionViewSource
dans un ItemsControl
qui est imbriqué dans un autre ItemsControl
. Je n'ai pas d'avertissement/d'erreur XAML, mais les données ne sont pas affichées. Si je lie la propriété ItemsSource
directement au ObservableCollection
, il n'y a aucun problème à afficher les éléments. Mon ViewModel est essentiellement une collection imbriquée dans une autre collection.Utilisation de CollectionViewSource dans un ItemsControl imbriqué dans un ItemsControl
XAML
<UserControl x:Class="View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:v="clr-namespace:ViewModel"
xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TabControl ItemsSource="{Binding Categories}" SelectedItem="{Binding SelectedCategory, Mode=TwoWay}">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate DataType="v:CategoryViewModel">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.ItemContainerStyle>
<TabControl.ContentTemplate>
<DataTemplate DataType="v:CategoryViewModel">
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Groups}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<CollectionViewSource x:Key="ValuesCollection" Source="{Binding Values}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</DataTemplate.Resources>
<GroupBox Header="{Binding Name}" >
<ItemsControl ItemsSource="{Binding Source={StaticResource ValuesCollection}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="10,2,10,2">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Name}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Value}""/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GroupBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</UserControl>
CategoryViewModel
namespace ViewModel
{
using System.Collections.ObjectModel;
using System.Diagnostics;
internal class CategoryViewModel : ObservableObjectBase
{
private string name;
private string id;
private int priority;
public CategoryViewModel()
{
this.Groups = new ObservableCollection<GroupViewModel>();
}
public ObservableCollection<GroupViewModel> Groups { get; }
public string Name
{
get
{
return this.name;
}
set
{
this.SetValue(ref this.name, value);
}
}
public string Id
{
get
{
return this.id;
}
set
{
this.SetValue(ref this.id, value);
}
}
public int Priority
{
get
{
return this.priority;
}
set
{
this.SetValue(ref this.priority, value);
}
}
}
}
GroupViewModel
namespace ViewModel
{
using System.Collections.ObjectModel;
using System.Diagnostics;
internal class GroupViewModel : ObservableObjectBase
{
private string name;
private string id;
private int priority;
public GroupViewModel()
{
this.Values = new ObservableCollection<ValueViewModel>();
}
public ObservableCollection<ValueViewModel> Values { get; }
public string Name
{
get
{
return this.name;
}
set
{
this.SetValue(ref this.name, value);
}
}
public string Id
{
get
{
return this.id;
}
set
{
this.SetValue(ref this.id, value);
}
}
public int Priority
{
get
{
return this.priority;
}
set
{
this.SetValue(ref this.priority, value);
}
}
}
}
ValueViewModel
namespace ViewModel
{
using System.Diagnostics;
internal class ValueViewModel : ObservableObjectBase
{
private string name;
private string id;
private string value;
private int priority;
public string Name
{
get
{
return this.name;
}
set
{
this.SetValue(ref this.name, value);
}
}
public string Id
{
get
{
return this.id;
}
set
{
this.SetValue(ref this.id, value);
}
}
public string Value
{
get
{
return this.value;
}
set
{
this.SetValue(ref this.value, value);
}
}
public int Priority
{
get
{
return this.priority;
}
set
{
this.SetValue(ref this.priority, value);
}
}
}
}
Une suggestion?
Merci d'avance.
Edit: Dans la sortie, je vois:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Values; DataItem=null; target element is 'CollectionViewSource' (HashCode=40230093); target property is 'Source' (type 'Object')
Merci. Je me sens stupide. Je ne sais pas comment j'ai raté cela .. – sergi
@sergi arrive ... l'information la plus importante était, que la liaison directement à l'ObservableCollection fonctionne, donc les sources d'erreur possibles où assez limité, même avec tout le code que vous avez posté;) – grek40
Merci pour l'explication détaillée :) – sergi