更改UI时未更新源

时间:2012-03-02 13:16:33

标签: wpf data-binding

将文本框绑定到工作站对象的值时出现问题。值正确显示,但是当我在屏幕上编辑值时,Source不会更新。我做错了什么?

    public LineControl(ArrayList cells)
    {


        InitializeComponent();
        this.cells = cells;
        foreach (Cell c in cells)
        {
            AccordionItem aci = new AccordionItem();

            StackPanel sp = new StackPanel();
            aci.Content = sp;
            DataGrid dg = new DataGrid();

            if(c.Stations!=null)
            foreach (Station s in c.Stations)
            {

                TextBox t = new TextBox();
                t.DataContext = s;
                 Binding binding = new Binding();
                 binding.Mode = BindingMode.TwoWay;
                 binding.Source = s;
                 binding.Path = new PropertyPath("Value");
                 binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                 t.SetBinding(TextBox.TextProperty, binding);
                 //t.TextChanged += new TextChangedEventHandler(t_TextChanged);

                sp.Children.Add(t);
            }
            acc.Items.Add(aci);
        }
}

我的电台课程类似

class Station
{
    public int Id { get; set; }
    public String Name { get; set; }
    public int Value { get; set;  }

}

在我的XAML中没有任何重要的内容:

     <UserControl x:Class="Knowles_ShiftreportEditor.LineControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:toolkit="clr-   namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="100">
<Grid>

    <toolkit:Accordion Width="100"  Name="acc" SelectionMode="One" Loaded="acc_Loaded">
    </toolkit:Accordion>
</Grid>

2 个答案:

答案 0 :(得分:2)

您是否在Station课程上试过了implementing INotifyPropertyChanged?此外,Stations循环顶部的foreach集合使用的类型是什么?

答案 1 :(得分:1)

您必须在Station课程中实施INotifyPropertyChanged界面。因此,您的Station类应该如下所示:

public class Station : INotifyPropertyChanged
{
    private int id;
    private String name;
    private int value;

    public int Id
    {
        get { return this.id; }
        set
        {
            this.id = value;
            NotifyPropertyChanged("Id");
        }
    }

    public String Name
    {
        get { return this.name; }
        set
        {
            this.name = value;
            NotifyPropertyChanged("Name");
        }
    }

    public int Value
    {
        get { return this.value; }
        set
        {
            this.value = value;
            NotifyPropertyChanged("Value");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

}