locked
MVVM Get object Id RRS feed

  • Question

  • <p>Hi!</p><p>I am tying to get the values of an object using mvvm. </p><p>The objec is created when a ListView item row is selected. Now the problem is that i can not access values to take object id. When i put the breakpoint i see that the values are under my object - SelectedPerson &gt; base. How can i get them?</p><p>This is my view</p>

    <ListView Grid.Row="1" ItemsSource="{Binding ProfilesList}" SelectedItem="{Binding SelectedPerson}" SelectionMode="Single">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Id" Width="150" DisplayMemberBinding="{Binding Id, Mode=OneWay}" />
                        <GridViewColumn Header="Full Name" Width="150" DisplayMemberBinding="{Binding Name}" />
                        <GridViewColumn Header="Username" Width="150" DisplayMemberBinding="{Binding Username}" />
                        <GridViewColumn Header="Email" Width="200" DisplayMemberBinding="{Binding Email}" />
                    </GridView>
                </ListView.View>
            </ListView>

    this is in my viewmodel

    private object _selecetdPerson;
            public object SelectedPerson
            {
                get { return _selecetdPerson; }
    
                set
                {
                        _selecetdPerson = value;
                        if (_selecetdPerson != value)
                        {
                            OnPropertyChanged("SelectedPerson");
                        }
    
                }
            }

    if i break i see that my SelectedPerson is

    SelectedPerson	{System.Data.Entity.DynamicProxies.Profile_CFB3461A4C7754ED1417C73DB9B6333FC7CEDD90B36D44B97456508039E4D1A5}
    and i tied to get the property like this
    var id = this.SelectedPerson.GetType().GetProperty("Id");
    which gives me the result
    id    {Int32 Id}    System.Reflection.PropertyInfo {System.Reflection.RuntimePropertyInfo}
    Thank you!


    Monday, December 22, 2014 1:31 PM

Answers

  • Just change type of the property from object to Profile if the type of your entity object is Profile, i.e. if the ComboBox contains Profile objects:


    private Profile _selecetdPerson;
            public Profile SelectedPerson
            {
                get { return _selecetdPerson; }
    
                set
                {
                        _selecetdPerson = value;
                        if (_selecetdPerson != value)
                        {
                            OnPropertyChanged("SelectedPerson");
                        }
    
                }
            }
    

    Please remember to mark helpful posts as answer and/or helpful.
    • Marked as answer by DarioOs81 Tuesday, December 23, 2014 9:36 AM
    Monday, December 22, 2014 3:34 PM
  • I didn't tell you to invent an object type.

    You put an object of some type into a collection and bind it to the itemssource of that listbox.

    Use the type of that thing.

    You have

    ItemsSource="{Binding ProfilesList}"

    I would expect you to have something like:

    public ObservableCollection<Person> ProfilesList {get; set;}
    SelectedPerson would be a Person rather than ProfilesList.

    private Person _selecetdPerson public Person SelecetdPerson { get { return _selecetdPerson; } set { _selecetdPerson = value; OnPropertyChanged("SelectedPerson");

    } }



    Please don't forget to upvote posts which you like and mark those which answer your question.
    My latest Technet article - Dynamic XAML

    • Edited by Andy ONeill Monday, December 22, 2014 3:41 PM
    • Marked as answer by DarioOs81 Monday, December 22, 2014 4:31 PM
    Monday, December 22, 2014 3:39 PM

All replies

  • Your item isn't created when you select an item - it's the same object as you put in your list the listbox is bound to.

    That seems to be an entity you have there.

    Don't you have a type for it?

    Isn't it a Person or something?

    SelectedPerson should be of that type.

    Remove  the if test, it's pointless.

    private Person _selecetdPerson

    public SelecetdPerson

    { get { return _selecetdPerson; } set { _selecetdPerson = value; OnPropertyChanged("SelectedPerson"); } }

    Assuming your Person ( or whatever it is ) has an Id then if you use that type you can just do.

    int i = _selectedPeson.Id;

    If you're using .net 4.5 you have callermembername and you can lose that magic string out the onpropertychanged.

    http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

        public class NotifyUIBase : INotifyPropertyChanged
        {
            // Very minimal implementation of INotifyPropertyChanged matching msdn
            // Note that this is dependent on .net 4.5+ because of CallerMemberName
            public event PropertyChangedEventHandler PropertyChanged;
            public void RaisePropertyChanged([CallerMemberName] String propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

    You can just copy the above into a class in your project, inherit your viewmodel from it and then you can do.

    private Person  _selecetdPerson
    
    public Person SelecetdPerson  
    
          {
                get { return _selecetdPerson; }
    
                set
                {
                        _selecetdPerson = value;
                         OnPropertyChanged(); 
               }
          }
    


    Please don't forget to upvote posts which you like and mark those which answer your question.
    My latest Technet article - Dynamic XAML


    • Edited by Andy ONeill Monday, December 22, 2014 3:39 PM
    Monday, December 22, 2014 2:33 PM
  • Hi Andy,

    i am using 4.5.1, Entity Framework and Prism V5 and i am extending my viewmodel with : BindableBase , so there is no need to write INotifyPropertyChanged again. Or? Because it works fine with everything else.

    The problem is that only if i create an object, when i select row it raises onpropertychanged. So before i had this

            private ProfilesList _selecetdPerson { get; set; }
            public ProfilesList SelectedPerson
            {
                get { return _selecetdPerson; }
    
                set
                {
                    _selecetdPerson = value;
                     OnPropertyChanged("SelectedPerson");
                }
            }


    and when i selected the row nothing happened. SelectedPerson was allways null. Than i got an answer to change it like this

            private object _selecetdPerson;
            public object SelectedPerson
            {
                get { return _selecetdPerson; }
    
                set
                {
                    _selecetdPerson = value;
    
                    if (_selecetdPerson != value)
                    {
                        OnPropertyChanged("SelectedPerson");
    
                    }
                }
            }

    and that worked with raising onpropertychanged event. The answer was provided here https://social.msdn.microsoft.com/Forums/vstudio/en-US/7c4e69b4-5e99-49a6-96e9-1ee5dfe644ff/prism-v5-mvvm-datagrid-listview-get-selecteditem-id?forum=wpf#f673d07e-cdde-409f-a228-7f5bee6691c7

    If i try like you suggested

    private ProfilesList _selecetdPerson
    
    public SelecetdPerson  
    
          {
                get { return _selecetdPerson; }
    
                set
                {
                        _selecetdPerson = value;
                         OnPropertyChanged(); 
               }
          }

    It gives me all kind of errorsm breaks my whole ViewModel and asks me to create class for SelectedPerson etc...

    Monday, December 22, 2014 3:02 PM
  • Just change type of the property from object to Profile if the type of your entity object is Profile, i.e. if the ComboBox contains Profile objects:


    private Profile _selecetdPerson;
            public Profile SelectedPerson
            {
                get { return _selecetdPerson; }
    
                set
                {
                        _selecetdPerson = value;
                        if (_selecetdPerson != value)
                        {
                            OnPropertyChanged("SelectedPerson");
                        }
    
                }
            }
    

    Please remember to mark helpful posts as answer and/or helpful.
    • Marked as answer by DarioOs81 Tuesday, December 23, 2014 9:36 AM
    Monday, December 22, 2014 3:34 PM
  • I didn't tell you to invent an object type.

    You put an object of some type into a collection and bind it to the itemssource of that listbox.

    Use the type of that thing.

    You have

    ItemsSource="{Binding ProfilesList}"

    I would expect you to have something like:

    public ObservableCollection<Person> ProfilesList {get; set;}
    SelectedPerson would be a Person rather than ProfilesList.

    private Person _selecetdPerson public Person SelecetdPerson { get { return _selecetdPerson; } set { _selecetdPerson = value; OnPropertyChanged("SelectedPerson");

    } }



    Please don't forget to upvote posts which you like and mark those which answer your question.
    My latest Technet article - Dynamic XAML

    • Edited by Andy ONeill Monday, December 22, 2014 3:41 PM
    • Marked as answer by DarioOs81 Monday, December 22, 2014 4:31 PM
    Monday, December 22, 2014 3:39 PM
  • Thank you Andy. Already got the same reply on my previous post here https://social.msdn.microsoft.com/Forums/vstudio/en-US/7c4e69b4-5e99-49a6-96e9-1ee5dfe644ff/prism-v5-mvvm-datagrid-listview-get-selecteditem-id?forum=wpf#f673d07e-cdde-409f-a228-7f5bee6691c7

    I didnt understood what i need to do :)

    • Marked as answer by DarioOs81 Tuesday, December 23, 2014 8:51 AM
    • Unmarked as answer by DarioOs81 Tuesday, December 23, 2014 9:37 AM
    Monday, December 22, 2014 4:31 PM
  • There's a lot to pick up with learning wpf and MVVM.

    PRISM is also a lot of learning.

    Problem solved though.

     

    Please don't forget to upvote posts which you like and mark those which answer your question.
    My latest Technet article - Dynamic XAML

    Monday, December 22, 2014 7:32 PM
  • Now i know. Didnt know what i was getting my self in to :) but it is great once things go the way you want :) Thanks for all you help. Will be asking allot for sure hahaha
    Monday, December 22, 2014 9:20 PM