<TextBox Text="{Binding Path=Property}" />
private static readonly DependencyProperty NumeratorProperty =
DependencyProperty.Register(nameof(Numerator), typeof(object), typeof(Fraction), new PropertyMetadata(null));
public object Numerator
{
get { return (object)GetValue(NumeratorProperty); }
set { SetValue(NumeratorProperty, value); }
}
public Fraction()
{
InitializeComponent();
DataContext = this;
}
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:t="clr-namespace:System.Threading;assembly=mscorlib"
DataSource={Binding}>
<StackPanel>
<TextBlock Text="{Binding Path=Numerator}" />
<Border Height="1" Margin="2" Background="Black" />
<TextBlock Text="{Binding Path=Denominator}" />
</StackPanel>
</Grid>
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:t="clr-namespace:System.Threading;assembly=mscorlib">
<TextBlock Text="{Binding Path=CurrentCulture, Source={x:Static t:Thread.CurrentThread}}" />
</Grid>
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<Grid.Resources>
<s:String x:Key="myValue">Hello World!</s:String>
</Grid.Resources>
<StackPanel>
<TextBlock Text="{Binding Source={StaticResource myValue}}" />
<TextBlock Text="{Binding Path=Length, Source={StaticResource myValue}}" />
</StackPanel>
</Grid>
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
DataContext="{x:Static s:Environment.OSVersion}">
<StackPanel>
<TextBlock Text="{Binding Platform}" />
<TextBlock Text="{Binding Version}" />
<TextBlock Text="{Binding ServicePack}" />
</StackPanel>
</Grid>
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
DataContext="{ViewModel}">
<StackPanel>
<TextBox Text="{Binding FirstName}" /> <!-- updates ViewModel on LostFocus event -->
<TextBox Text="{Binding LastName, UpdateSourcheTrigger=PropertyChanged}" /> <!-- updates ViewModel on every property change -->
</StackPanel>
</Grid>
// in constructor
InitializeComponent();
var person = new Person { ... };
DataContext = person;
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:MyNamespace">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Person}">
<Grid DataContext="{ViewModel}">
<StackPanel>
<TextBox Text="{Binding FirstName}" /> <!-- updates ViewModel on LostFocus event -->
<TextBox Text="{Binding LastName, UpdateSourcheTrigger=PropertyChanged}" /> <!-- updates ViewModel on every property change -->
</StackPanel>
</Grid>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ContentControl Content="{Binding}" />
</StackPanel>
</Window>
// in constructor
InitializeComponent();
var people = new List<Person> { ... };
DataContext = people;
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:MyNamespace">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Person}">
<Grid DataContext="{ViewModel}">
<StackPanel>
<TextBox Text="{Binding FirstName}" /> <!-- updates ViewModel on LostFocus event -->
<TextBox Text="{Binding LastName, UpdateSourcheTrigger=PropertyChanged}" /> <!-- updates ViewModel on every property change -->
</StackPanel>
</Grid>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{Binding}" />
</StackPanel>
</Window>