views:

66

answers:

2

Hi! While learning c# and wpf, i am trying to get grasp of data binding. So far unsuccessful. All the help i could find on the net, even if described as "for beginners", is too complicated for me to begin to understand. I would appreciate if someone can provide a code for binding in a very simple example:

namespace BindingTest
{
    class TestClass
    {
        public int testProperty;
        public TestClass()
        {
            testProperty = 10;
        }
    }
}

namespace BindingTest
{
    public partial class Window1 : Window
    {
        TestClass iTestClass = new TestClass();
        public Window1()
        {
            InitializeComponent();
        }
        private void buttonAdd10_Click(object sender, RoutedEventArgs e)
        {
            iTestClass.testProperty += 10;
        }
    }
}

<Window x:Class="BindingTest1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBox Height="25" 
                 Margin="52,0,130,89" 
                 Name="textBox1" 
                 VerticalAlignment="Bottom" 
        />
        <Button Height="34" 
                HorizontalAlignment="Left" 
                Margin="38,40,0,0" 
                Name="buttonAdd10" 
                VerticalAlignment="Top" 
                Width="62" 
                Click="buttonAdd10_Click">
        +10</Button>
   </Grid>    
</Window>

All I want to do is to bind textBox1.Text to iTestClass.testProperty, so that when I click the button I can see its value change in the textbox. What changes should be made in the code of this simple example in order to accomplish that?

If it is possible to be done without INotifyPropertyChanged, that is the way I wanna do it. Thank you in advance!

Vladimir

+3  A: 

To bind to a property in WPF you have two options.

a) INotifyPropertyChanged
b) DependencyProperty

Since you don't want to use INotifyPropertyChanged, you're left with DependencyProperty.

In the code-behind of MainWindow, add this line in the constructor:

this.DataContext = iTestClass;

In the XAML of MainWindow, add this attribute to the TextBox:

Text="{Binding testProperty}"

Change TestClass as follows.

class TestClass : DependencyObject
{
    public int testProperty
    {
        get { return (int)GetValue(TestPropertyProperty); }
        set { SetValue(TestPropertyProperty, value); }
    }

    public static readonly DependencyProperty TestPropertyProperty =
        DependencyProperty.Register("testProperty", typeof(int), typeof(TestClass));

    public TestClass()
    {
        testProperty = 10;
    }
}

If you'd prefer the INotifyPropertyChanged version, change TestClass to this (the rest is the same):

class TestClass : INotifyPropertyChanged
{
    private int _testProperty;
    public int testProperty
    {
        get { return _testProperty; }
        set
        {
            if (_testProperty == value)
                return;

            _testProperty = value;

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("testProperty"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public TestClass()
    {
        testProperty = 10;
    }
}

Why do you need this? Well WPF has no way of telling if the property has changed unless you tell it somehow.

The two ways WPF can tell this is with an event, which INotifyPropertyChanged provides, or by registering the property within the WPF system, which is how the DependencyProperty version works.

If you don't do one of these then WPF won't be able to tell when the value of the property changed, and so binding won't work.

Cameron MacFarland
thank you, I will look into the dependecyproperty way of doing it, that is what i had in mind.BTW, I tried doing it with inotifypropertychanged, and i had pretty much the same thing you give me here, but i always get error: 'BindingTest1.TestClass1' does not implement interface member 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged', which seems very odd. Waht could be the reason for that?
butaro
ok, forget about the error, i figured out my mistake.
butaro
A: 

I was getting the same error for ages ('System.ComponentModel.INotifyPropertyChanged.PropertyChanged') and it took several goes before i released i was incorrectly implementing "propertyChanged", not "PropertyChanged" - just in case that helps anyone else.

frozen