Skip to content

Instantly share code, notes, and snippets.

@WillSullivan
Created August 26, 2015 17:24
Show Gist options
  • Save WillSullivan/f535ede65eda4a9d3632 to your computer and use it in GitHub Desktop.
Save WillSullivan/f535ede65eda4a9d3632 to your computer and use it in GitHub Desktop.
SSCCE
1. create a new wpf project called ContentControlDataTemplate
2. open MainWindow.xaml
3. Remove all text, paste the following
<Window x:Class="ContentControlDataTemplate.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:ContentControlDataTemplate"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:t="clr-namespace:ContentControlDataTemplate"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type t:Foo}">
<StackPanel>
<Label>It worked</Label>
<TextBlock Text="{Binding Text}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<ContentControl Content="{Binding}"/>
</Window>
4. open MainWindow.xaml.cs
5. remove all text, paste the following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ContentControlDataTemplate
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new Foo { Text = "lol" };
}
}
public class Foo
{
public string Text { get; set; }
}
}
6. Run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment