Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Are you using <code>ObservableCollection</code> and does your model implement <code>INotifyPropertyChanged</code> these two things will automaticly update the ListBox on any change. no need to explicitly refresh the list.</p> <p>Here is a small example of using <code>ObservableCollection</code> and <code>INotifyPropertyChanged</code>, obviously you will populate your ObservableCollection from your <code>SQL</code> database.</p> <p>Window:</p> <pre><code>public partial class MainWindow : Window, INotifyPropertyChanged { private ObservableCollection&lt;MyModel&gt; _list = new ObservableCollection&lt;MyModel&gt;(); private MyModel _selectedModel; public MainWindow() { InitializeComponent(); List.Add(new MyModel { Name = "James", CompanyName = "StackOverflow"}); List.Add(new MyModel { Name = "Adam", CompanyName = "StackOverflow" }); List.Add(new MyModel { Name = "Chris", CompanyName = "StackOverflow" }); List.Add(new MyModel { Name = "Steve", CompanyName = "StackOverflow" }); List.Add(new MyModel { Name = "Brent", CompanyName = "StackOverflow" }); } public ObservableCollection&lt;MyModel&gt; List { get { return _list; } set { _list = value; } } public MyModel SelectedModel { get { return _selectedModel; } set { _selectedModel = value; NotifyPropertyChanged("SelectedModel"); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } } </code></pre> <p>Xaml</p> <pre><code>&lt;Window x:Class="WpfApplication11.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Name="UI"&gt; &lt;Grid&gt; &lt;ListBox ItemsSource="{Binding ElementName=UI, Path=List}" SelectedItem="{Binding ElementName=UI, Path=SelectedModel}" Margin="0,0,200,0" DisplayMemberPath="DisplayMember" SelectedIndex="0" /&gt; &lt;StackPanel HorizontalAlignment="Left" Height="100" Margin="322,10,0,0" VerticalAlignment="Top" Width="185"&gt; &lt;TextBlock Text="Name" /&gt; &lt;TextBox Height="23" TextWrapping="Wrap" Text="{Binding ElementName=UI, Path=SelectedModel.Name, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;TextBlock Text="Company Name" /&gt; &lt;TextBox Height="23" TextWrapping="Wrap" Text="{Binding ElementName=UI, Path=SelectedModel.CompanyName, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Model</p> <pre><code>public class MyModel : INotifyPropertyChanged { private string _name; private string _companyName; public string Name { get { return _name; } set { _name = value; NotifyPropertyChanged("Name"); } } public string CompanyName { get { return _companyName; } set { _companyName = value; NotifyPropertyChanged("CompanyName"); } } public string DisplayMember { get { return string.Format("{0} ({1})", Name, CompanyName); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); PropertyChanged(this, new PropertyChangedEventArgs("DisplayMember")); } } } </code></pre> <p>In this case any edit to properties will Update your list instantly, also will update when new Items are added/removed.</p> <p><img src="https://i.stack.imgur.com/24XPJ.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload