Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

ObservableCollection and Selected Item(s)

$
0
0

I am trying to wrap my head around some more advanced binding. This is in part an example for others, so I left out some uneeded stuff (like the check if properties really change) for better readability.

This are the Classes of my excuse for ViewModel:

    public class Character : INotifyPropertyChanged
    {
        private string _Title;

        private string _FirstName;

        private string _LastName;

        private string _Description;

        private CollectionOfFilms _Films;

        public string Title
        {
            get { return _Title; }
            set { _Title = value;
              RaisePropertyChanged(this, "Title");
            }
        }
        public string FirstName
        {
            get { return _FirstName; }
            set { _FirstName = value;
                RaisePropertyChanged(this, "FirstName");
            }
        }

        public string LastName
        {
            get { return _LastName; }
            set { _LastName = value;
                RaisePropertyChanged(this, "LastName");
            }
        }

        public string Description
        {
            get { return _Description; }
            set { _Description = value;
                RaisePropertyChanged(this, "Description");
            }
        }

        public CollectionOfFilms Films
        {
            get { return _Films; }
            set { _Films = value;
                RaisePropertyChanged(this, "Films");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(object sender, string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    //Small workaround to allow defining an ObservableCollection<Character> in XAML
    public class CollectionOfCharacters : ObservableCollection<Character> { }


    public class Film : INotifyPropertyChanged
    {
        private DateTime _firstShown;

        private string _FilmName;

        public DateTime FirstShown
        {
            get { return _firstShown; }
            set { _firstShown = value;
                RaisePropertyChanged(this, "FirstShown");
            }
        }

        public string FilmName
        {
            get { return _FilmName; }
            set { _FilmName = value;
                RaisePropertyChanged(this, "FilmName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(object sender, string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    //Same workaround again
    public class CollectionOfFilms : ObservableCollection<Film> { }

Here I create and fill an isntance in my App.xaml:

<Application x:Class="WPFGridViewTemplate.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:app="clr-namespace:WPFGridViewTemplate"
             StartupUri="MainWindow.xaml"><Application.Resources><app:CollectionOfCharacters x:Key="LOC"><app:Character Title="Darth" FirstName="Anakin" LastName="Skywalker"
                        Description="Went really Drakside"><app:Character.Films><app:CollectionOfFilms><app:Film FirstShown="01/01/1977" FilmName="EP 4 - A new Hope"/><app:Film FirstShown="01/01/1980" FilmName="EP 5 - The Empire Strikes back"/><app:Film FirstShown="01/01/1983" FilmName="EP 6 - Return of hte Jedi"/></app:CollectionOfFilms></app:Character.Films></app:Character><app:Character Title="Dr." FirstName="John" LastName="Doolitle"
                        Description="A guy who speak with animals"><app:Character.Films><app:CollectionOfFilms><app:Film FirstShown="01/01/1967" FilmName="Doctor Doolitle (Rex Harison)"/><app:Film FirstShown="01/01/1998" FilmName="Dr. Doolitle (Edi Murphy)"/><app:Film FirstShown="01/01/2001" FilmName="Dr. Doolitle 2"/></app:CollectionOfFilms></app:Character.Films></app:Character></app:CollectionOfCharacters></Application.Resources></Application>

And here is finally my Window.xaml:

<Window x:Class="WPFGridViewTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:app="clr-namespace:WPFGridViewTemplate"
        Title="MainWindow"><DockPanel><DataGrid x:Name="outerGrid" IsSynchronizedWithCurrentItem="True" 
                  ItemsSource="{StaticResource ResourceKey=LOC}"><DataGrid.RowDetailsTemplate><DataTemplate><DataGrid x:Name="innerGrid" 
                              IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=Films}"/></DataTemplate></DataGrid.RowDetailsTemplate></DataGrid><DataGrid x:Name="unrelatedGrid"
            IsSynchronizedWithCurrentItem="True" ItemsSource="{StaticResource ResourceKey=LOC}"></DataGrid></DockPanel></Window>

So far it works as anticipated.

Now my problem/target:

I want to let "unrelatedGrid" show what is currently shown in "innerGrid". Wich would mean I have to (Dynamically?) Bind it to the CurrentItem. The problem is: There is nothing even remotely similar to a "selectedItem(s)" property in an ObservableCollection.

Yet both DataGrids are able to write/read a selectedItem that is "someplace". Otherwise they could not Synchronise (I doubt they do uggly stuff like "outerGrid" and "unrelatedGrid" directly talking to one another).

So where do these dataGrids read & write this selectedItem inforamtion and how can I get to it actively?


Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>