0

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.

<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}"
         >
<UserControl.Resources >
    <DataTemplate x:Key="custTemplate" >
        <StackPanel >
            <Image Source="{Binding ImagePath}"/>
            <TextBlock Text="{Binding MyTitle}"/>
        </StackPanel>          
    </DataTemplate>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="{x:Null}" >
    <Button Content="Button" Height="316" 
            HorizontalAlignment="Left" Margin="12,2,0,0" 
            Name="button1" VerticalAlignment="Top" 
            Width="423" Click="button1_Click" 
            ContentTemplate="{DynamicResource custTemplate}"
            />
    <Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="441,2,0,0" Name="button2" VerticalAlignment="Top" Width="208" Click="button2_Click" />
    <Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="669,2,0,0" Name="button3" VerticalAlignment="Top" Width="208" />
    <Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="441,162,0,0" Name="button4" VerticalAlignment="Top" Width="208" />
    <Button Content="Button" Height="156" HorizontalAlignment="Left" Margin="669,162,0,0" Name="button5" VerticalAlignment="Top" Width="208" />
</Grid>


Model:

public class Video
    {
        public string  MyTitle { get; set; }
        public string ImagePath { get; set; }
    }

ViewModel

  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;
        }
    }
Fabii
  • 3,820
  • 14
  • 51
  • 92
  • How does your `VideoViewModel` look like? Because in the `UserControl`'s `DataContext` you are binding to the `VideoViewModel`, which is not the `Video` class itself but contains (I guess) a property with type `Video`. – nemesv Jun 26 '12 at 05:04
  • Check this link for Binding with Button using MVVM : http://stackoverflow.com/questions/3772797/wpf-mvvm-button-control-binding-in-datatemplate – Ponmalar Jun 26 '12 at 05:14
  • When you run your program, have a look at the Output panel in Visual Studio. Are there any BindingExpression errors? – Greg Sansom Jun 26 '12 at 05:16
  • @nemesv I just updated the code with the ViewModel – Fabii Jun 26 '12 at 05:40
  • @GregSansom no there are no errors – Fabii Jun 26 '12 at 05:40

2 Answers2

1

If you really want to do this using a DataTemplate, then:

<Button>
        <Button.Content>
            <x:Type TypeName="Visual"/>
        </Button.Content>
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <ContentPresenter ContentSource="Content"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
        <Button.ContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Name}"/>
            </DataTemplate>
        </Button.ContentTemplate>
    </Button>

Your ViewModel binding should be okay to use here so in the DataTemplate you can just tweak it to include your Image and TextBlock.

EdFred
  • 661
  • 4
  • 10
0

DataTemplate in resources:

<DataTemplate x:Key="bTemplate">
        <Button Label="{Binding MyTitle}" 
                Command="{Binding Execute}"> 
           <Image Source="{Binding ImagePath}"   />
        </Button>
 </DataTemplate>

Not exactly as in your question but then you can use a control that takes an ItemSource such as a ListView:

<ListView 
  ItemsSource="{Binding ListData}"  
  ItemTemplate="{StaticResource bTemplate}" 
>
</ListView>

I added a Command , since it is necessary if you want to implement MVVM rather than event click.

call_stack
  • 21
  • 5
  • I've implemented it using ListView and ListBox before, but now I need to implement just a Button specifically. i.e no ListView or other Layouts. – Fabii Jun 26 '12 at 14:38