Click here to Skip to main content
15,033,917 members
Home / Discussions / WPF
   

WPF

 
QuestionC# WPF listbox binding to selected item does not update my property Pin
Member 128805954-Dec-16 5:54
MemberMember 128805954-Dec-16 5:54 
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
MemberMember 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
MemberMember 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
MemberMember 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 
So I mocked up a DB and used your code with some optimizations I explain in comments. Works for me. If you're still having an issue it might be a DB issue or some code that isn't posted. Here's the code I used:

MainWindow.xaml: Nothing important changed, just some formatting for my screen
<Window x:Class="ContactsList.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel LastChildFill="True">
        <ListBox x:Name="ContactsLstBx" Width="200" ItemsSource="{Binding ContactList}" SelectedItem="{Binding SelectedContact, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Left">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ListBox x:Name="ConversationLstBx" Width="200" ItemsSource="{Binding ConversationList}" DockPanel.Dock="Right" HorizontalAlignment="Right" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Title}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </DockPanel>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
    public MainViewModel ViewModel { get; private set; }

    public MainWindow()
    {
        InitializeComponent();
        ViewModel = new MainViewModel();
        DataContext = ViewModel;
    }
}
MainViewModel.cs
public class MainViewModel : ViewModelBase
{<br />
    //The collections can just be auto-implemented properties since we have no reason
    //to use custom logic like PropertyChanged.  Remember, the view doesn't listen for that.
    public ObservableCollection<Contact> ContactList { get; private set; }
    public ObservableCollection<Conversation> ConversationList { get; private set; }
    private Contact selectedContact;
    public Contact SelectedContact
    {
        get
        {
            return selectedContact;
        }
        set
        {
            if (selectedContact != value)
            {
                selectedContact = value; 
                PopulateConversationList();
                RaisePropertyChanged();
            }
        }
    }
    public MainViewModel()
    {
        ContactList = new ObservableCollection<Contact>(cbdc.Contacts);
        ConversationList = new ObservableCollection<Conversation>();
        SelectedContact = ContactList.FirstOrDefault();
        //Don't need the PopulateConversationList() call because it's already called
        //when setting SelectedContact
    }

    public void PopulateConversationList()
    {
        //Just optimized the if-statement a bit since Clear() is called regardless
        ConversationList.Clear();
        if (selectedContact != null)
        {
            /*
            var conversations = from c in cbdc.Conversations
                                where c.ContactID == SelectedContact.Id
                                select c;
            */
            //My simulated database call, use your call in your application
            var conversations = cbdc.GetConversations(selectedContact);

            foreach (Conversation c in conversations)
                ConversationList.Add(c);
        }
    }
}
ViewModelBase.cs Not really important, just me mocking up DB objects
public class ViewModelBase : INotifyPropertyChanged
{
    protected MockDB cbdc;
    public event PropertyChangedEventHandler PropertyChanged;
    public ViewModelBase()
    {
        cbdc = new MockDB();
    }
    protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public class MockDB
{
    private Dictionary<Contact, List<Conversation>> conversations;
    public List<Contact> Contacts { get; private set; }
    public MockDB()
    {
        Contacts = CreateContacts();
        conversations = CreateConversations(Contacts);
    }
    public List<Conversation> GetConversations(Contact c) =>
        conversations[c];
    private List<Contact> CreateContacts()
    {
        return new List<Contact>()
        {
            new Contact("Jim"),
            new Contact("Bob"),
            new Contact("Mary")
        };
    }
    private Dictionary<Contact, List<Conversation>> CreateConversations(List<Contact> contacts)
    {
        Dictionary<Contact, List<Conversation>> conversationMap = new Dictionary<Contact, List<Conversation>>();
        foreach (Contact c in contacts)
            conversationMap.Add(c, CreateConversationList(c));
        return conversationMap;
    }
    private List<Conversation> CreateConversationList(Contact contact)
    {
        return new List<Conversation>()
        {
            new Conversation($"Hi {contact.Name}!"),
            new Conversation("Pleasant day today.")
        };
    }
}

public class Contact
{
    public string Name { get; private set; }

    public Contact(string contactName)
    {
        Name = contactName;
    }
}

public class Conversation
{
    public string Title { get; private set; }

    public Conversation(string conversationText)
    {
        Title = conversationText;
    }
}

Hopefully this helps Smile | :)
GeneralRe: C# WPF listbox binding to selected item does not update my property Pin
Member 128805957-Dec-16 8:10
MemberMember 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
MemberMember 128805955-Dec-16 9:10 
QuestionTabControl views are not disposed Pin
Leif Simon Goodwin23-Nov-16 4:55
mvaLeif 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
mvaLeif 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
MemberAshish 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
MemberAshish khanduri20-Nov-16 19:50 
QuestionSound Command that can intercept the Active Thread Pin
Vimalsoft(Pty) Ltd15-Nov-16 17:56
professionalVimalsoft(Pty) Ltd15-Nov-16 17:56 
AnswerRe: Sound Command that can intercept the Active Thread Pin
Gerry Schmitz16-Nov-16 6:07
mveGerry Schmitz16-Nov-16 6:07 
QuestionCan't Move Label with mouse in WPF Pin
Dadou5512-Nov-16 22:44
MemberDadou5512-Nov-16 22:44 
AnswerRe: Can't Move Label with mouse in WPF Pin
Richard Deeming14-Nov-16 2:13
mveRichard Deeming14-Nov-16 2:13 
QuestionHow to achieve grouping and ungrouping usercontrol in wpf application programmatically Pin
Pankaj Deharia10-Nov-16 3:05
professionalPankaj Deharia10-Nov-16 3:05 
AnswerRe: How to achieve grouping and ungrouping usercontrol in wpf application programmatically Pin
Gerry Schmitz10-Nov-16 7:01
mveGerry Schmitz10-Nov-16 7:01 
QuestionHow to solve the echo in a MVVM project with external data updates Pin
ronald6231-Oct-16 0:16
Memberronald6231-Oct-16 0:16 

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.