Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to bind data correctly to a WPF button using MVVM
    text
    copied!<p>Here is my setup, it seems I am not binding the data correctly. The outcome should be a Button with an Image and text displayed. Nothing is displayed.</p> <pre><code>&lt;UserControl x:Class="AmebaPrototype.UI.LandingPivot.featureControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="330" d:DesignWidth="960" DataContext="{Binding Source={StaticResource viewModelLocator}, Path=VideoViewModel}" &gt; &lt;UserControl.Resources &gt; &lt;DataTemplate x:Key="custTemplate" &gt; &lt;StackPanel &gt; &lt;Image Source="{Binding ImagePath}"/&gt; &lt;TextBlock Text="{Binding MyTitle}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/UserControl.Resources&gt; &lt;Grid x:Name="LayoutRoot" Background="{x:Null}" &gt; &lt;Button Content="Button" Height="316" HorizontalAlignment="Left" Margin="12,2,0,0" Name="button1" VerticalAlignment="Top" Width="423" Click="button1_Click" ContentTemplate="{DynamicResource custTemplate}" /&gt; &lt;Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="441,2,0,0" Name="button2" VerticalAlignment="Top" Width="208" Click="button2_Click" /&gt; &lt;Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="669,2,0,0" Name="button3" VerticalAlignment="Top" Width="208" /&gt; &lt;Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="441,162,0,0" Name="button4" VerticalAlignment="Top" Width="208" /&gt; &lt;Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="669,162,0,0" Name="button5" VerticalAlignment="Top" Width="208" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <hr> <p>Model:</p> <pre><code>public class Video { public string MyTitle { get; set; } public string ImagePath { get; set; } } </code></pre> <hr> <p>ViewModel</p> <pre><code> public ViewModel VideoViewModel { get { if(viewmodel == null) { viewmodel = new ViewModel(); viewmodel.ListData.Clear(); viewmodel.ListData.Add(new Video { MyTitle = "Title1", ImagePath = "exampleimage.com/image.png" }); viewmodel.ListData.Add(new Video { MyTitle = "Title2", ImagePath = "exampleimage.com/image.png" }); } return viewmodel; } } </code></pre>
 

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