Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a XAML binding to a member variable?

Tags:

c#

xaml

I'm 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's sake I just made a program that displays a counter as well as a button to increment the counter.

Based on earlier threads I've 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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int Counter
        {
            get { return (int)GetValue(CounterProperty); }
            set { SetValue(CounterProperty, value); }
        }
        public static readonly DependencyProperty CounterProperty = 
            DependencyProperty.Register("Counter", typeof(int), typeof(MainWindow), new PropertyMetadata(null));

        public MainWindow()
        {
            Counter = 0;
            InitializeComponent();
        }

        private void incrementCounter(object sender, RoutedEventArgs e)
        {
            ++Counter;
        }
    }
}

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 the TextBlock isn't showing a counter value. How do I wire the TextBlock to the Counter member in a correct way?

like image 974
Pieter Avatar asked Nov 20 '12 10:11

Pieter


People also ask

How does binding work in XAML?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

How many types of binding are there in WPF?

WPF binding offers four types of Binding.

What is two way binding WPF?

Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.


1 Answers

Try adding a "Name" to your window and binding using "ElementName"

<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="350" Width="525" Name="UI">
    <Grid>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
            <TextBlock x:Name="txbCounter" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ElementName=UI, Path=Counter}" VerticalAlignment="Top"/>
            <Button x:Name="btnIncrement" Content="Increment" Width="75" Click="incrementCounter"/>
        </StackPanel>
    </Grid>

</Window>
like image 83
sa_ddam213 Avatar answered Sep 30 '22 08:09

sa_ddam213