WPF databinding to a Size property

二次信任 提交于 2019-12-10 11:56:33

问题


I have a problem with databinding in WPF, here's my exemplary class:

public class testClass
{
    public Size testInnerSize;

    public testClass()
    {
        testInnerSize = new Size(66, 99);
    }
}

I want to bind a TextBox in my form to the property of testInnerSize, let's say Width. So I'm setting the DataContext of this textbox to the testClass object and in XAML:

<TextBox Text="{Binding Path=testInnerSize.Width }" Name="textBox3" (...)

But it does not work, the textbox is empty instead of having value 66. On the other hand, when I set the DataContext to testObject.testInnerSize the value is displayed in a textbox, yet it is not updated in the object after text modification.

So the question is: how can I two-way bind the Width property of a Size object that is a property of another object?

Full code used for testing:

public partial class testpage : Page
{
    public Size testSize;
    testClass testObject = new testClass();

    public testpage()
    {
        InitializeComponent();
        testSize = new Size(6, 9);
        textBox2.DataContext = testSize;
        textBox3.DataContext = testObject;
    }

    private void textChanged(object sender, TextChangedEventArgs e)
    {
        MessageBox.Show(testObject.testInnerSize.Width + " " + testObject.testInnerSize.Height, "", MessageBoxButton.OK);
    }
}

public class testClass
{
    public Size testInnerSize;

    public testClass()
    {
        testInnerSize = new Size(66, 99);
    }
}

XAML binding:

    <TextBox Text="{Binding Path=Width }" Name="textBox2" />
    <TextBox Text="{Binding Path=testInnerSize.Width }"  Name="textBox3" TextChanged="textChanged" />

UPDATE 1

Now I have checked and it does not depend on being a subobject of the testClass object - the testSize property is binded so its textbox displays a proper value, but the testSize.Width value is not being updated. To see it just add:

    private void text1Changed(object sender, TextChangedEventArgs e)
    {
        MessageBox.Show(testSize.Width + " " + testSize.Height, "", MessageBoxButton.OK);
    }

handler to a TextChanged event.


回答1:


Your testInnerSize is not a property. It's a field. Properties have get and set accessors. Also you should implement INotifyPropertyChanged.

Properties (C# Programming Guide)

Data Binding Overview




回答2:


So first, if you would like to use Databinding in WPF, you should declare a Property other than a Field. So your testClass should look like this:

public class testClass
{
    public Size testInnerSize
    {
        get;
        set;
    }

    public testClass()
    {
        testInnerSize = new Size(66, 99);
    }
}

And if you would like to take the advantage of two-way binding, you need to introduce INotifyPropertyChanged interface.

public class testClass : INotifyPropertyChanged
{

    private Size _testInnerSize;

    public event PropertyChangedEventHandler PropertyChanged;

    public Size testInnerSize
    {
        get
        {
            return this._testInnerSize;
        }
        set
        {
            this._testInnerSize = value;
            if (null != PropertyChanged)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("testInnerSize"));
            }
        }
    }

    public testClass()
    {
        testInnerSize = new Size(66, 99);
    }
}

For detailed information, you could check the links in first answer.



来源:https://stackoverflow.com/questions/28743992/wpf-databinding-to-a-size-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!