如何将 ObservableCollection 绑定到 DataTemplate 中的 TextBox? [英] How can I bind an ObservableCollection to TextBoxes in a DataTemplate?

查看:23
本文介绍了如何将 ObservableCollection 绑定到 DataTemplate 中的 TextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个 ObservableCollection 成功绑定到 DataTemplate 中的 TextBox.我可以让数据正确显示,但我无法通过 UI 更改列表数据.我有一个名为model"的模型类,其中包含一个名为List"的 ObservableCollection.该类实现 INotifyPropertyChanged 接口.这是 shell 的 xaml.Window1 网格的 DataContext 设置为theGrid.DataContext=model"

I am trying to successfully TwoWay bind an ObservableCollection to TextBoxes in a DataTemplate. I can get the data to display properly, but I am unable to change the list data through the UI. I have a Model class named 'model' which contains an ObservableCollection named 'List'. The class implements the INotifyPropertyChanged interface. Here is the xaml for the shell. The DataContext for Window1's grid is set to "theGrid.DataContext=model"

<Window x:Class="BindThat.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindThat"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="theGrid">
    <GroupBox BorderBrush="LightGreen">
        <GroupBox.Header>
            <TextBlock Text="Group" />
        </GroupBox.Header>
        <ItemsControl ItemsSource="{Binding Path=List}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </GroupBox> 
</StackPanel>

这是模型类的代码:

class Model : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    private ObservableCollection<string> _list = new ObservableCollection<string>();
    public ObservableCollection<string> List
    {
        get { return _list; }
        set 
        { 
            _list = value;
            NotifyPropertyChanged("List");
        }
    }

    public Model()
    {
        List.Add("why");
        List.Add("not");
        List.Add("these?");
    }
}

有人可以建议我是否以正确的方式进行处理吗?

Could anyone advise if I am going about this the correct way?

推荐答案

你需要一个属性来绑定两种方式,所以字符串不适合这个.

You need a property to bind two way, so string is not good for this.

将其包裹在一个字符串对象中,如下所示:

Wrap it in a string object, like this:

public class Model
{
    public ObservableCollection<StringObject> List { get; private set; }
    public Model()
    {
        List = new ObservableCollection<StringObject>
                   {
                       new StringObject {Value = "why"},
                       new StringObject {Value = "not"},
                       new StringObject {Value = "these"},
                   };
    }
}

public class StringObject
{
    public string Value { get; set; }
}

并绑定到 Value 属性而不是."

and bind to Value property instead of "."

此外,您不需要通知可观察集合的更改,因此在您的模型具有自己的其他一些属性之前,它不需要具有 INotifyPropertyChange.如果您希望 ItemsControl 对单个 StringObject 中的更改做出反应,则应将 INotifyPropertyChanged 添加到 StringObject.

Also, you don't need to notify of a change in observable collection, so until your model has some other propertis of its own, it does not need to have INotifyPropertyChange. If you want your ItemsControl react to changes in the individual StringObjects, then you should add INotifyPropertyChanged to a StringObject.

再说一次,双向绑定是默认的,所以你只需要

And yet again, two way binding is default, so you need only

<TextBox Text="{Binding Path=Value}" />

在您的绑定中.

这篇关于如何将 ObservableCollection 绑定到 DataTemplate 中的 TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆