Set DataContext in XAML Set DataContext in XAML wpf wpf

Set DataContext in XAML


Just change the Window to bind DataContext to itself:

<Window x:Class="WpfApplication2.Window1"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Window1" Height="300" Width="300"        DataContext="{Binding RelativeSource={RelativeSource Self}}" ... />


I believe the DataContext in this scenario is implicit and does not have to be set since you are using the code behind. If you were using MVVM you would add a reference to that folder and class inside of you XAML markup and set the resource key equal to a value that can then be declared as the DataContext inside of a child elements DataContext property. But in your case (since you are not using MVVM) you shouldn't have to do that.