Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Data binding Label content

Tags:

People also ask

How do I add a label in WPF?

Creating a WPF LabelThe Width and Height attributes of the Label element represent the width and the height of a Label. The Content property of the Label element sets the text of a Label. The Name attribute represents the name of the control, which is a unique identifier of a control.

How to bind data in WPF?

Specifying the path to the valueIf your binding source is an object, you use the Binding. Path property to specify the value to use for your binding. If you're binding to XML data, you use the Binding. XPath property to specify the value.

How to bind property in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

What is Data binding in c#?

The concept of databinding is quite simple; It allows you to 'bind' the data that is contained in an object to a visual control. That control 'displays' your data. When the user changes the value that is displayed by the control, the changes are automatically persisted to the underlying object.


I'm trying to create a simple WPF Application using data binding. The code seems fine, but my view is not updating when I'm updating my property. Here's my XAML:

<Window x:Class="Calculator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:calculator="clr-namespace:Calculator"
        Title="MainWindow" Height="350" Width="525"
        Name="MainWindowName">
    <Grid>
        <Label Name="MyLabel" Background="LightGray" FontSize="17pt" HorizontalContentAlignment="Right" Margin="10,10,10,0" VerticalAlignment="Top" Height="40" 
               Content="{Binding Path=CalculatorOutput, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

Here's my code-behind:

namespace Calculator
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            DataContext = new CalculatorViewModel();
            InitializeComponent();
        }
    }
}

Here's my view-model

namespace Calculator
{
    public class CalculatorViewModel : INotifyPropertyChanged
    {
        private String _calculatorOutput;
        private String CalculatorOutput
        {
            set
            {
                _calculatorOutput = value;
                NotifyPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            var handler = PropertyChanged;
            if (handler != null)
               handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

I'm can't see what I am missing here?? o.O