I am trying to learn c# and WPF application. Here I am trying to redirect from one WPF page(MainWindow.xaml) to another(HandWash.xaml) on a button click event. But the following code is throwing NULLReferenceException.
This is the MainWindow.xaml file.
<Window x:Class="MyApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
d:DesignHeight="720" d:DesignWidth="1284"
Title="StartPage" WindowStartupLocation="CenterScreen" WindowStyle="None" WindowState="Maximized" Closed="Window_Closed">
<Window.Background>
<ImageBrush ImageSource="/Images/StartPage.png"></ImageBrush>
</Window.Background>
<Grid>
<Button Content="Hand Wash" Height="794" HorizontalAlignment="Left" Name="HandWash" VerticalAlignment="Top" Width="353" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="HandWash_Click"/>
<Button Content="Bathing" Height="794" HorizontalAlignment="Left" Margin="390,0,0,0" Name="Bathing" VerticalAlignment="Top" Width="301" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="Bathing_Click"/>
<Button Content="Nail-Clip" Height="794" HorizontalAlignment="Left" Margin="730,0,0,0" Name="NailClip" VerticalAlignment="Top" Width="295" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="NailClip_Click"/>
<Button Content="Teeth Brush" Height="794" HorizontalAlignment="Left" Margin="1067,0,0,0" Name="TeethBrush" VerticalAlignment="Top" Width="310" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="TeethBrush_Click"/>
</Grid>
</Window>
Background code for this:
private void TeethBrush_Click(object sender, RoutedEventArgs e)
{
try
{
TeethBrush teeth = new TeethBrush(myarg);
NavigationService navService = NavigationService.GetNavigationService(this);
navService.Navigate(teeth); // NULL REFERENCE EXCEPTION at this line
}
catch (NullReferenceException ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
This is the code for TeethBrush.xaml :
<Page x:Class="MyApplication.TeethBrush"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
d:DesignHeight="720" d:DesignWidth="1284"
Title="TeethBrush">
<Grid>
</Grid>
<Page.Background>
<ImageBrush ImageSource="C:\Users\Tonmoy\Documents\Visual Studio 2010\Projects\MyKinectApp\MyKinectApp\Images\StartPage.png"></ImageBrush>
</Page.Background>
</Page>
and the background code is:
public TeethBrush(Myargs arg)
{
InitializeComponent();
//Rest of the code
}
Please help....
You need to have a frame in main window where contents of Pages will be hosted.
If you add the following namespace to MainWindow:
xmlns:local="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
you can define a frame somewhere, e.g. in your grid:
<Grid>
<local:Frame x:Name="mainFrame">
</local:Frame>
....
Then you can navigate from your event handler like so:
TeethBrush teeth = new TeethBrush(myarg);
this.mainFrame.Navigate(teeth);
Related
I have a problem where I run my program and it gives me a STA Error on my "Test" Window. It does not have an error if I don't have a timer going from the Page I am running till the window. I'll show and Example:
public Page1()
{
InitializeComponent();
Task.Delay(2000).ContinueWith(_ =>
{
var page = new TestW();
page.Show();
}
);
}
This is from my Page1 to open up my TestW ( Test Window )
My main code looks like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Luk_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Button_Click(object sender, RoutedEventArgs e) // Login
{
Main.Content = new Page1();
Framep.Visibility = Visibility.Visible;
GridS.Visibility = Visibility.Hidden;
}
}
And my XAML Code:
<Window x:Class="date_app.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:date_app"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="400"
ResizeMode="NoResize"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
AllowsTransparency="True"
Background="Transparent"
>
<Border BorderBrush="Black"
BorderThickness="1.5"
CornerRadius="10"
>
<Border.Background>
<ImageBrush x:Name="ImageBrush" ImageSource="Images\bgapp.png" Stretch="None"/>
</Border.Background>
<Grid>
<StackPanel x:Name="Framep">
<Frame x:Name="Main" />
</StackPanel>
<StackPanel x:Name="GridS">
<!--X Luk Knappen.-->
<Grid>
<Button BorderBrush="Transparent" Name="Luk" Margin="360, 10, 10, 0" Background="Transparent" Click="Luk_Click">
<Button.Content>
<Image Source="Images\Ikke-navngivet.png" Height="20" Width="35" IsHitTestVisible="False" />
</Button.Content>
</Button>
</Grid>
<Grid>
<Image Source="Images\DateL.png" Height="200"/>
</Grid>
<!--Username.-->
<Grid>
<TextBox Style="{StaticResource WatermarkTextbox}" Name="Email" Height="30" Width="300" FontSize="20" FontFamily="Comic Sans MS" Text="Hello" Margin="0 40 0 0" TextChanged="Email_TextChanged"/>
</Grid>
<!--Password.-->
<Grid>
<TextBox Style="{StaticResource WatermarkTextbox1}" Name="Pass" Height="30" Width="300" FontSize="20" FontFamily="Comic Sans MS" Text="Hello" Margin="0 35 0 0" TextChanged="Pass_TextChanged"/>
</Grid>
<!--Login Knap.-->
<Grid>
<Button Grid.Column="0" Content="Log ind" Width="80" Height="30" FontSize="20" Margin="0 100 0 0" Click="Button_Click" />
</Grid>
<!--Opret Bruger-->
<Grid>
<Button Grid.Column="0" Content="Opret Bruger" Width="140" Height="30" FontSize="20" Margin="0 20 0 0" />
</Grid>
</StackPanel>
</Grid>
</Border>
Page1 Code:
<Page x:Class="date_app.Page1"
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"
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
xmlns:local="clr-namespace:date_app"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="400"
Title="LoadingM">
<Border BorderBrush="Black"
BorderThickness="1.5"
CornerRadius="10"
>
<Border.Background>
<ImageBrush x:Name="ImageBrush" ImageSource="Images\bgapp.png" Stretch="None"/>
</Border.Background>
<Grid>
<TextBlock Text="LOADING. . ." FontSize="50" FontFamily="Comic Sans MS"/>
<Image gif:AnimationBehavior.SourceUri="Images\CatGif.gif" />
</Grid>
</Border>
The problem I have: If I run with the Task Delay My test Window gets an STA Error that I don't know how to fix.
If I run without the Task Delay It just opens it all up but no STA Error.
I am trying to do this:
When I press on the Login button It is supposed to open the Page1 and hide the main window for a " Loading " Screen and then after the delay to close Main window + Page1 down to Open up the New Window. Been sitting with this for a little while :) Thanks in advance!
-- Test Window Is plain. No code there.
This is the Background.
The problem is that the Action given to ContinueWith will not be exectued on the UiThread but on a free ThreadPool-Thread, so the new TestW object will be constructed and used by a non STA-Thread. But any WPF-UI-component must be constructed and used by an STA-Thread.
The solution is to replace your Task.Delay() with
Task.Delay(2000).ContinueWith(_ =>
{
Dispatcher.Invoke(() =>
{
var page = new TestW();
page.Show();
});
});
This will delegate your execution back to the UiThread.
I try to make a "email messenger". I have two windows. Main window, there is ribbon with bookmarks and one button (New message). This button invoke second window NewMessage. The Mainwindow is owner of the NewMessage window.
MainWindow:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
NewMessage newmessage = new NewMessage();
newmessage.Show();
}
}
}
MainWindow XAML:
<Window x:Class="Messenger.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Messenger"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="New Message" Height="20" Width="80
" HorizontalAlignment="Left" Margin="35,46,0,0" VerticalAlignment="Top" Click="Button_Click" />
<TabControl HorizontalAlignment="Left" Height="254" Margin="34,44,0,0" VerticalAlignment="Top" Width="461" Background="{x:Null}" SelectedIndex="1">
<TabItem Header="Delivered" Visibility="Hidden" Width="80">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="Delivered" Visibility="Visible" Width="80">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="Sent" Width="80">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="Trash" Width="80">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
NewMessage:
public partial class NewMessage : Window
{
public NewMessage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
NewMessage XAML:
<Window x:Class="Messenger.NewMessage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Messenger"
mc:Ignorable="d"
Title="NewMessage" Height="351.047" Width="814.398">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="27*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="23*"/>
<RowDefinition Height="24*"/>
<RowDefinition Height="243*"/>
<RowDefinition Height="31*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="Komu" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
<Label Grid.Row="1" Content="Předmět" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
<RichTextBox HorizontalAlignment="Left" Height="243" Grid.Row="2" VerticalAlignment="Top" Width="806" Grid.ColumnSpan="2">
<FlowDocument>
<Paragraph>
<Run Text="Sem zadejte text zprávy"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button Grid.Row="3" Content="Odeslat" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Width="75" Margin="0,5,90,0"/>
<Button Grid.Row="3" Content="Storno" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Width="75" Margin="0,5,10,0" Click="Button_Click"/>
</Grid>
I would like to, when I minimize NewMessage window, this window minimize to the bottom of MainWindow "taskbar". If you know gmail, there is the same principle. Any idea?
<Window x:Class="ErasmusWPF.Marta"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ErasmusWPF"
mc:Ignorable="d"
Title="Marta" Height="768" Width="1366" ResizeMode="NoResize" WindowState="Maximized" AllowsTransparency="True" WindowStyle="None" Background="Transparent">
<Grid>
<Image Source="Images\grass.jpg" Stretch="UniformToFill" Grid.ColumnSpan="2"/>
<Image Source="Images\rail_luxury_shift-a.png" Height="833" VerticalAlignment="Bottom" OpacityMask="Black" Margin="-717,-38,-862,-27" Grid.ColumnSpan="2"/>
<Image Source="Images\Untitled.png" Margin="234,0,-369,-125" Width="1400" Height="767.8"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="636,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="566" LineHeight="18" TextAlignment="Center" FontSize="30"><Run Text="Klausīties Valka mūziku "/><LineBreak/><Run Text="(""/><Run Text="Dis"/><Run Text="t"/><Run Text="a"/><Run Text="nt Light")"/><LineBreak/><Run Text=" "/><InlineUIContainer/></TextBlock>
<Button x:Name="button5" Content="" Margin="636,27,624,682" Click="button_Click">
<Button.Background>
<ImageBrush ImageSource="Images/bcyEgxxzi.png"/>
</Button.Background>
</Button>
<Button x:Name="play" HorizontalAlignment="Left" Margin="484,322,0,0" VerticalAlignment="Top" Width="140" Height="84" Click="play_Click" >
<Button.Background>
<ImageBrush ImageSource="Images/playbutton.png"/>
</Button.Background>
<MediaElement x:Name="mediaElement" HorizontalAlignment="Left" Height="84" VerticalAlignment="Top" Width="140" LoadedBehavior="Manual" UnloadedBehavior="Stop" Source="C:\Users\Leonards\Documents\Visual Studio 2015\Projects\ErasmusWPF\ErasmusWPF\ayylmao\crysis3.wmv" RenderTransformOrigin="0.5,0.5" Stretch="Fill" >
</MediaElement>
</Button>
<Button x:Name="play2" HorizontalAlignment="Left" Margin="669,322,0,0" VerticalAlignment="Top" Width="140" Height="84" Click="play2_Click" >
<Button.Background>
<ImageBrush ImageSource="Images/playbutton.png"/>
</Button.Background>
</Button>
<MediaElement x:Name="mediaElement2" HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="1368" LoadedBehavior="Manual" UnloadedBehavior="Stop" Source="C:\Users\Leonards\Documents\Visual Studio 2015\Projects\ErasmusWPF\ErasmusWPF\ayylmao\crysis3.wmv" Margin="0,0,-2,0" Stretch="Fill" />
</Grid>
</Window>
This is my xaml code
public Marta()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
System.Media.SoundPlayer player =
new System.Media.SoundPlayer();
player.SoundLocation = #"C:\Users\Leonards\Documents\visual studio 2015\Projects\ErasmusWPF\ErasmusWPF\Music\DistantLight.wav";
player.Load();
player.Play();
}
private async void play_Click(object sender, RoutedEventArgs e)
{
mediaElement.Play();
await Task.Delay (183000);
mediaElement.Source = null;
}
private async void play2_Click(object sender, RoutedEventArgs e)
{
mediaElement2.Play();
await Task.Delay(183000);
mediaElement2.Source = null;
}
}
The problem begins when i try to run application on any other computer. Mediaelements are programmed for my computers C directory. Whenever louched on any other pc, the content is lost and missing.
I'm trying to create a little tool for Microsofts Team Founation Server (which is not that important for the question). However i'm not that familiar with C# and WPF and even after reading through some tutorials about bindings and resources, i can't figure out, how to get the code working.
With the Visual Studio Designer i created a little form, that just has some buttons and a textbox that should display the name of the authorized user.
Unfortunately, after logging in, an ArgumentException is thrown. So the question is : How can i bind my local TfsTeamProjectCollection to tfsTeamProjectCollectionViewSource?
Thanks for your help!
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TFSBranchingTool"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:Client="clr-namespace:Microsoft.TeamFoundation.Client;assembly=Microsoft.TeamFoundation.Client" x:Class="TFSBranchingTool.MainWindow"
mc:Ignorable="d"
Title="TFSBranchingTool" Height="360" Width="560"
x:Name="wnd" Loaded="wnd_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="tfsTeamProjectCollectionViewSource" d:DesignSource="{d:DesignInstance {x:Type Client:TfsTeamProjectCollection}, CreateList=True}" Source="{Binding}"/>
</Window.Resources>
<Grid Margin="0,0,0,-1">
<Menu x:Name="menu" VerticalAlignment="Top" Background="{DynamicResource {x:Static SystemColors.MenuBrushKey}}" d:IsLocked="True">
<MenuItem Header="File">
<MenuItem Header="Exit" HorizontalAlignment="Left" Click="exit_application_click"/>
</MenuItem>
<MenuItem Header="Team">
<MenuItem Header="Connect to server" HorizontalAlignment="Left" Margin="0" Width="201" Click="connect_to_server_click"/>
</MenuItem>
</Menu>
<StatusBar x:Name="statusbar" VerticalAlignment="Bottom" Margin="0,0,0,-2" MinHeight="16">
<StatusBarItem x:Name="connection_status" Content="{Binding TeamProjectCollection.AuthorizedIdentity.DisplayName}" HorizontalAlignment="Left"/>
</StatusBar>
<Grid x:Name="grid1" DataContext="{StaticResource tfsTeamProjectCollectionViewSource}" HorizontalAlignment="Left" Margin="138,122,0,0" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Display Name:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
<TextBox x:Name="displayNameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding AuthorizedIdentity.DisplayName, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
</Grid>
</Grid>
And here is the logic:
using Microsoft.TeamFoundation.Client;
using System.Windows;
namespace TFSBranchingTool
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private TfsTeamProjectCollection m_tfs_team_project_collection;
public MainWindow()
{
InitializeComponent();
}
private void exit_application_click(object sender, RoutedEventArgs e)
{
Close();
}
private void connect_to_server_click(object sender, RoutedEventArgs e)
{
TeamProjectPicker team_project_picker = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);
if (team_project_picker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
m_tfs_team_project_collection = team_project_picker.SelectedTeamProjectCollection;
System.Windows.Data.CollectionViewSource tfsTeamProjectCollectionViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tfsTeamProjectCollectionViewSource")));
//ArgumentException :
//Additional Information: "<TFS-URI>" is an invalid value for the property "Source".
tfsTeamProjectCollectionViewSource.Source = m_tfs_team_project_collection;
}
}
}
}
You have to use GetDefaultView static Method of CollectionViewSource class to give the view your collection.
Here is what you have to do.
tfsTeamProjectCollectionViewSource.Source = CollectionViewSource.GetDefaultView(m_tfs_team_project_collection);
Also you haven't set the data context of the window as Window itself.
Try doing this.
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
By doing the above, any binding in the xaml code will look for its source in the window.
Another thing I have found wrong in your code is that you have defined
tfsTeamProjectCollectionViewSource as a local variable rather than a data member of the Window.
Try making it a data member like m_tfs_team_project_collection and then see what happens.
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.