Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Made some minor changes to your code as explained below.</p> <pre><code> class ListingItem { public string title { get; set; } public string user { get; set; } public string category { get; set; } //Dummy constructor for test purposes public ListingItem() { title = "TestTitle"; user = "TestUser"; category = "TestCatagory"; } } </code></pre> <ul> <li>The list item class, I changed the title, user and category to <strong>properties</strong> (get;set;). I also needed to make them public so they could be accessed through the binding.</li> </ul> <pre><code> class ListMaker { public static List getListing() { List listing = new List(); for (int i = 0; i &lt; 100; i++) { listing.Add(new ListingItem()); } return listing; } } </code></pre> <ul> <li>No changes to your ListMaker class</li> </ul> <pre><code> public class CommandHandler : ICommand { private Action _action; private bool _canExecute; public CommandHandler(Action action, bool canExecute=true) { _action = action; _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { _action(); } } </code></pre> <ul> <li>I introduced a new class to be able to bind the button. This kind of class if relatively common</li> </ul> <pre><code> &lt;Window x:Class=&quot;SimpleDatabinding.MainWindow&quot;<br/> xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;<br/> xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;<br/> xmlns:viewmodel=&quot;clr-namespace:SimpleDatabinding&quot;<br/> Title=&quot;MainWindow&quot; Height=&quot;350&quot; Width=&quot;525&quot;&gt;<br/> <br/> &lt;Window.DataContext&gt;<br/> &lt;viewmodel:MainWindowViewModel/&gt;<br/> &lt;/Window.DataContext&gt;<br/><br/> &lt;Grid&gt;<br/> &lt;DockPanel&gt;<br/> &lt;Button Command=&quot;{Binding FillListCommand}&quot; DockPanel.Dock=&quot;Top&quot;&gt;Fill List&lt;/Button&gt;<br/><br/> &lt;ListBox ItemsSource=&quot;{Binding Listing}&quot; DockPanel.Dock=&quot;Top&quot;&gt;<br/> &lt;ListBox.ItemTemplate&gt;<br/> &lt;DataTemplate&gt;<br/> &lt;StackPanel Orientation=&quot;Vertical&quot;&gt;<br/> &lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;<br/> &lt;TextBlock Foreground=&quot;Gray&quot; Margin=&quot;25,0,0,0&quot; Text=&quot;{Binding user}&quot;/&gt;<br/> &lt;TextBlock Foreground=&quot;Gray&quot; Margin=&quot;25,0,0,0&quot; Text=&quot;{Binding category}&quot;/&gt;<br/> &lt;/StackPanel&gt;<br/> &lt;TextBlock Foreground=&quot;Black&quot; Width=&quot;270&quot; TextWrapping=&quot;Wrap&quot; Text=&quot;{Binding title}&quot;/&gt;<br/> &lt;/StackPanel&gt;<br/> &lt;/DataTemplate&gt;<br/> &lt;/ListBox.ItemTemplate&gt;<br/> &lt;/ListBox&gt;<br/> &lt;/DockPanel&gt;<br/> &lt;/Grid&gt;<br/>&lt;/Window&gt; </code></pre> <p><ul> <li>Note the addition of <b>xmlns:viewmodel="clr-namespace:SimpleDatabinding"</b>. SimpleDatabinding was the name of the project. It's used to locate the view model in the datacontext below. </li> <li>The Window.DataContext binds the WPF page to the view model. I called my class MainWindowViewModel (see below). This will automatically create an instance of the view model and bind it to the window.</li> <li>I introduced a button to click. It's bound to a command <B>FillListCommand</B>. I'll define that in the view model below.</li> <li>I updated the <B>ItemsSource</B> on the <B>ListBox</B> to be bound to the <B>Listing</B> property.</li> <li>Other than that, I think it's the same.</li> </ul></p> <pre><code> class MainWindowViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public List Listing { get; set; } public CommandHandler FillListCommand { get; set; } public MainWindowViewModel() { FillListCommand = new CommandHandler(DoFillList); } public void DoFillList() { Listing = ListMaker.getListing(); ProperyHasChanged("Listing"); } private void ProperyHasChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre> <p><ul> <li>Finally in the viewmodel class, I implemented the <B>INotifyPropertyChanged</B> interface. This is the mechanism to notify the UI that a value on your view model has changed. In most implementations, this is wrapped in some sort of ViewModel base class but I left it in so you could see it.</li> <li>As above, I converted the <B>Listing</B> variable to a public property (get;set;) so it could be accessed through the binding.</li> <li>I created a <B>CommandHandler</B> property called <B>FillListCommand</B>. This uses the class above. The button is bound to this variable. The constructor of the view model initializes and points it to the function to be called when the button is clicked.</li> <li>Finally, in the <B>DoFillList</B> function, I initialize <B>Listing</B> as you had it but I also use the notification to let the UI know it's changed.</li> </ul> </p> <p> Sorry about all the writing. Hope this is somewhat helpful. I don't think it's too different from what you had. </p>
    singulars
    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.
 

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