I am new to XAML and data binding. I want to define a GUI control in MainWindow.xaml that gets its data from a member variable in MainWindow.xaml.cs . For simplicity, I just created a program that displays a counter, as well as a button to increase the counter.
Based on earlier threads , I looked up, I came up with the following code:
MainWindow.xaml.cs
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 XAMLBindingTest {
MainWindow.xaml
<Window x:Class="XAMLBindingTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="140" Width="180"> <Grid> <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top"> <TextBlock x:Name="txbCounter" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Counter}" VerticalAlignment="Top"/> <Button x:Name="btnIncrement" Content="Increment" Width="75" Click="incrementCounter"/> </StackPanel> </Grid> </Window>
This example compiles, but TextBlock does not show the value of the counter. How to connect a TextBlock element to a Counter element?
source share