简体   繁体   中英

How to bind data correctly to a WPF button using MVVM

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;
        }
    }

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM