Click here to Skip to main content
15,888,136 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: UserControl Pin
Jon McKee5-Dec-16 11:56
professionalJon McKee5-Dec-16 11:56 
AnswerRe: UserControl Pin
Gerry Schmitz5-Dec-16 11:29
mveGerry Schmitz5-Dec-16 11:29 
GeneralRe: UserControl Pin
Meshack Musundi5-Dec-16 22:23
professionalMeshack Musundi5-Dec-16 22:23 
GeneralRe: UserControl Pin
Kevin Marois6-Dec-16 4:49
professionalKevin Marois6-Dec-16 4:49 
GeneralRe: UserControl Pin
Meshack Musundi6-Dec-16 5:04
professionalMeshack Musundi6-Dec-16 5:04 
AnswerRe: UserControl Pin
Richard Deeming6-Dec-16 2:10
mveRichard Deeming6-Dec-16 2:10 
GeneralRe: UserControl Pin
Kevin Marois6-Dec-16 4:47
professionalKevin Marois6-Dec-16 4:47 
QuestionC# WPF listbox binding to selected item does not update my property Pin
Member 128805954-Dec-16 5:54
Member 128805954-Dec-16 5:54 
Hello!
I am trying to make a Contact Book, where i can have my contacts and conversations(+other activities related to them) in C# WPF.I am also trying to learn MVVM with this.

I have 2 listboxes each bound to an observable collection populated from a database using LinqToSQL.
Listbox 1(ContactsLstBx) is displaying a list of contact names, and in Listbox 2(ConversationLstBx), i want to display the conversations i had with the selected contact in Listbox 1.


The problem i have is that(besides being a "noob") the SelectedContact property, does not update after i set it in MainViewModel constructor. If i set it manually it displays the conversation list correctly based on the ContactId.

What i am trying to achieve is: when the program starts, the only listbox loaded is the ContactsLstBxd after i select a contact, to get the SelectedContact, and load the other listbox.
Should i use a SelectionChanged event? Can i load the conversation listbox if i add a call to PopulateConversationList() in my SelectedContact setter?

Any help/advice is greatly apreciated.

Thanks!



C#:
C#
public class MainViewModel : ViewModelBase
    {
        private Contact selectedContact;
 
        public ObservableCollection<Contact> ContactList;
        public ObservableCollection<Conversation> ConversationList;
 
        public MainViewModel()
        {
            ContactList = new ObservableCollection<Contact>(cbdc.Contacts);
            SelectedContact = ContactList.FirstOrDefault();
            PopulateConversationList();


        }

        public Contact SelectedContact
        {
            get { return selectedContact; }
            set
            {
                if (selectedContact == value)
                    return;

                    selectedContact = value;
                    RaisePropertyChanged("SelectedContact");
            }
        }

        public void PopulateConversationList()
        {
            if (selectedContact != null)
            {
                var conversations = from c in cbdc.Conversations
                                    where c.ContactID == SelectedContact.Id
                                    select c;
                ConversationList = new ObservableCollection<Conversation>(conversations);
            }
         }


C#
public class ViewModelBase: INotifyPropertyChanged
{
    public ContactLinqToSQLClassesDataContext cbdc = new ContactLinqToSQLClassesDataContext();


    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;
    internal void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}





XAML:
XML
<ListBox x:Name="ContactsLstBx" HorizontalAlignment="Left" Height="289" Margin="10,50,0,0" VerticalAlignment="Top" Width="115" ItemsSource="{Binding ContactList}" SelectedItem="{Binding SelectedContact, UpdateSourceTrigger=PropertyChanged}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ListBox x:Name="ConversationLstBx" HorizontalAlignment="Left" Height="134" Margin="333,50,0,0" VerticalAlignment="Top" Width="115" ItemsSource="{Binding ConversationList}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Title}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

AnswerRe: C# WPF listbox binding to selected item does not update my property Pin
Richard Deeming5-Dec-16 1:58
mveRichard Deeming5-Dec-16 1:58 
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Member 128805955-Dec-16 9:08
Member 128805955-Dec-16 9:08 
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Jon McKee5-Dec-16 10:59
professionalJon McKee5-Dec-16 10:59 
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Member 128805956-Dec-16 7:38
Member 128805956-Dec-16 7:38 
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Jon McKee6-Dec-16 8:23
professionalJon McKee6-Dec-16 8:23 
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Member 128805956-Dec-16 9:10
Member 128805956-Dec-16 9:10 
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Jon McKee6-Dec-16 10:30
professionalJon McKee6-Dec-16 10:30 
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Member 128805957-Dec-16 8:10
Member 128805957-Dec-16 8:10 
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Jon McKee5-Dec-16 10:13
professionalJon McKee5-Dec-16 10:13 
AnswerRe: C# WPF listbox binding to selected item does not update my property Pin
Gerry Schmitz5-Dec-16 5:51
mveGerry Schmitz5-Dec-16 5:51 
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Member 128805955-Dec-16 9:10
Member 128805955-Dec-16 9:10 
QuestionTabControl views are not disposed Pin
Leif Simon Goodwin23-Nov-16 4:55
Leif Simon Goodwin23-Nov-16 4:55 
AnswerRe: TabControl views are not disposed Pin
Pete O'Hanlon23-Nov-16 5:30
mvePete O'Hanlon23-Nov-16 5:30 
PraiseRe: TabControl views are not disposed Pin
Leif Simon Goodwin23-Nov-16 21:09
Leif Simon Goodwin23-Nov-16 21:09 
QuestionIs there any way to automate the silverlight application using Eclipse-testng-selenium-java frameowork Pin
Ashish khanduri20-Nov-16 18:23
Ashish khanduri20-Nov-16 18:23 
AnswerRe: Is there any way to automate the silverlight application using Eclipse-testng-selenium-java frameowork Pin
Mycroft Holmes20-Nov-16 19:39
professionalMycroft Holmes20-Nov-16 19:39 
GeneralRe: Is there any way to automate the silverlight application using Eclipse-testng-selenium-java frameowork Pin
Ashish khanduri20-Nov-16 19:50
Ashish khanduri20-Nov-16 19:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.