Binding Basics (WPF)
Binding Basics: Unfortunately this note will not be for raw beginners with no understanding of WPF.
Let’s Look carefully:
• We want to the TextBlock to show us: (i) a string stored in a Resource and (ii) The Operating System Version.
• These values are obtained with data binding.
• We must specify the property we are binding to using the Path property, and the Source using the Source Property. We must bind to an element in the control.
• If we are using a resource we must use the StaticResource key of the relevant resource.
• If we are using a system property we must get it as a static variable.
See below for examples:
// Shows the namespaces and the resources
<Window x:Class="WpfApplication1.MainWindow"
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:t="clr-namespace:System.Threading;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<s:String x:Key="val">I will follow_him</s:String>
</Window.Resources>
</Window>
// Shows databinding
<Grid>
<StackPanel>
<!--<TextBlock Text="{Binding Path=CurrentCulture, Source={x:Static t:Thread.CurrentThread }}" ></TextBlock>-->
<TextBlock Text="{Binding Path=Length, Source={StaticResource val}}"></TextBlock>
<TextBlock Text="{Binding Path=VersionString, Source={x:Static s:Environment.OSVersion}}"></TextBlock>
</StackPanel>
</Grid>
Written on June 21, 2017