数据绑定到自制类中的属性

时间:2014-11-08 01:19:18

标签: .net wpf c#-4.0 visual-studio-2013

我目前正在学习一些关于WPF的内容,但我无法理解DataBindings。我尝试过在StackOverflow和MSDN上找到的很多解决方案,但是没有人会按我的意愿去做。

我有以下结构:

C#-Class:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDataBindings
{
    internal class TextStore
    {
        internal ObservableCollection<string> Collection { get; set; }
        internal string Text { get; set; }

        internal TextStore()
        {
            this.Text = "Hello World!!!!";
            this.Collection = new ObservableCollection<string> { "hello", "World", "!!!!" };
        }
    }
}

XAML:

<Window x:Class="TestDataBindings.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestDataBindings"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:TextStore x:Key="TextStorage"></local:TextStore>
    </Window.Resources>
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="160,108,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Source={StaticResource TextStorage}, Path=Text}"/>
        <ListBox HorizontalAlignment="Left" Height="100" Margin="305,73,0,0" VerticalAlignment="Top" Width="100" ItemsSource="{Binding Source={StaticResource TextStorage}, Path=Collection}"/>

    </Grid>
</Window>

现在我希望Hello World !!!!出现在我的TextBox和三个字符串中以弹出到我的ListBox中,但这些都不会发生。谁能告诉我这里做错了什么?

1 个答案:

答案 0 :(得分:0)

首先,您需要使用TextStore类的实例设置MainWindow的DataConetxt:

var textStore = new TextStore();
this.DataContext = textStore;

然后,为了通知Ui已经对它们绑定的属性所做的更改,您需要实现INotifyPropertyChanged接口,并且需要在每个属性设置器中调用OnpropertyChanged方法:

 internal class TextStore : INotifyPropertyChanged
{
    private ObservableCollection<string> _collection;
    public ObservableCollection<string> Collection
    {
        get
        {
            return _collection;
        }
        set
        {
            if (_collection == value)
            {
                return;
            }

            _collection = value;
            OnPropertyChanged();
        }
    }
    private string _text;
    public string Text
    {
        get
        {
            return _text;
        }

        set
        {
            if (_text == value)
            {
                return;
            }

            _text = value;
            OnPropertyChanged();
        }
    }

    internal TextStore()
    {
        this.Text = "Hello World!!!!";
        this.Collection = new ObservableCollection<string> { "hello", "World", "!!!!" };
    }

    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}