2012-03-02 58 views
1

將文本框綁定到工作站對象的值時出現問題。值正確顯示,但是當我編輯屏幕上的值源不會更新。我究竟做錯了什麼?更改UI時未更新源

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> 

+0

如果你的意思是在代碼上一站的變化不會反映在UI然後根據@avanek說,它INotifyPropertyChanged的。如果不是,請同時顯示XAML – kaj 2012-03-02 13:30:20

+0

我已經插入了我的XAML,但沒有什麼值得注意的... – TheJoeIaut 2012-03-02 13:35:27

+0

但是哪種方式無法使用? 1.屏幕上的更改未反映在代碼中或2.代碼中的更改未反映在屏幕上? – kaj 2012-03-02 14:11:52

回答

1

您必須實施INotifyPropertyChanged接口Station類。所以,你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)); 
     } 
    } 

} 
+0

感謝,但這並沒有解決我的問題呢。我覺得你有我的問題錯了:在UI的更改不會反映到源,而不是周圍的其他方式。 – TheJoeIaut 2012-03-02 13:41:22

+0

我在一個正常的文本框試了一下,這是工作的罰款。 – Amit 2012-03-02 13:48:23