Skip to content

Instantly share code, notes, and snippets.

@Feddas
Created January 28, 2015 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Feddas/6ce6739ce3b23a1c8535 to your computer and use it in GitHub Desktop.
Save Feddas/6ce6739ce3b23a1c8535 to your computer and use it in GitHub Desktop.
<Window x:Class="ProjectName.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewmodel="clr-namespace:ProjectName.ViewModels"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!-- This creates an instance of the TestViewModel held in the ProjectName.ViewModels namespace -->
<viewmodel:TestViewModel x:Key="TestViewModel" />
</Window.Resources>
<Grid x:Name="LayoutRoot" DataContext="{StaticResource TestViewModel}">
<TextBlock Text="{Binding TestProp}" x:Name="txtBlock" />
</Grid>
</Window>
using ProjectName.ViewModels;
namespace ProjectName
{
public partial class MainWindow : Window
{
/// <summary>
/// This property is if the ViewModel needs to be accessed in this codebehind
/// </summary>
private TestViewModel ViewModel
{
get
{
return (this.LayoutRoot.DataContext as TestViewModel);
}
}
public MainWindow()
{
InitializeComponent();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment