Récupérer une collection liée à une CollectionViewSource
Bonjour,
Je souhaite effectuer une vue Maitre-Détail entre deux DataGrids.
Pour cela, j'utilise une CollectionViewSource afin de connaître le SelectedItem.
Voici le XAML correspondant au DataGrid "Maitre" et sa ressource :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <UserControl.Resources>
<CollectionViewSource x:Key="masterSourceCVS" Source="{Binding Mode=TwoWay, Path=Sources}"/>
</UserControl.Resources>
<!-- Master -->
<tk:DataGrid x:Name="dgSourceBlender" ItemsSource="{Binding Source={StaticResource masterSourceCVS}}" AutoGenerateColumns="False" AllowDrop="True" DragOver="dgSourceBlender_DragOver" Drop="dgSourceBlender_Drop">
<tk:DataGrid.Columns>
<tk:DataGridTemplateColumn Header="Type"/>
<tk:DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" IsReadOnly="True" Width="SizeToCells" MinWidth="50"/>
<tk:DataGridTextColumn Header="Parser" IsReadOnly="True" Width="SizeToCells" MinWidth="50"/>
<tk:DataGridTextColumn Header="Status" IsReadOnly="True" Width="SizeToCells" MinWidth="50"/>
</tk:DataGrid.Columns>
</tk:DataGrid> |
Pour l'instant tout va bien :)
Mais j'ai besoin d'accèder à ma ObservableCollection _sources via ma property "Sources"
Code:
1 2 3 4 5 6
| private ObservableCollection<Source> _sources;
public ObservableCollection<Source> Sources {
get { return _sources; }
set { _sources = value; }
} |
Cette ObservableCollection est associée à ma CollectionViewSource "masterSourceCVS" afin d'y ajouter des Objects lors d'un Drag&Drop qui s'effectue dans le code behind.
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| private void dgSourceBlender_Drop(object sender, DragEventArgs e) {
//Drag & Drop form windows exlorer
string[] fileNames = e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string fileName in fileNames) {
Uri uri = new Uri(fileName, UriKind.RelativeOrAbsolute);
//Ajout des ojbects de type Source lors du Drop dans mon DataGrid
((ObservableCollection<Source>)dgSourceBlender.ItemsSource).Add(new Source(uri.ToString(), "type", "path"));
}
.... |
L'opération fonctionnait bien sans la CollectionViewSource :
Code:
1 2
|
<tk:DataGrid x:Name="dgSourceBlender" ItemsSource="{Binding Mode=TwoWay, Path=Sources}" AutoGenerateColumns="False" AllowDrop="True" DragOver="dgSourceBlender_DragOver" Drop="dgSourceBlender_Drop"> |
Donc si je me trompe pas, j'ai juste besoin de récupérer ma ObservableCollection "Sources" gâce à l'ItemSource à partir de ma CollectionViewSource.
Mais comment faire ? That is the question ;)
Merci :)