0

在我的WP8應用程序中,我有一個類,它有一個名爲Matrix的ObservableCollection<ObservableCollection<int>>屬性。Xaml文本框與可觀察集合的雙向綁定

我想使用項目控件顯示這些矩陣。

<ItemsControl ItemsSource="{Binding FirstMatrix.Matrix}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <ItemsControl ItemsSource="{Binding}"> 
         <ItemsControl.ItemsPanel> 
          <ItemsPanelTemplate> 
           <StackPanel Orientation="Horizontal"></StackPanel> 
          </ItemsPanelTemplate> 
         </ItemsControl.ItemsPanel> 
         <ItemsControl.ItemTemplate> 
          <DataTemplate> 
           <TextBox Text="{Binding}" /> 
          </DataTemplate> 
         </ItemsControl.ItemTemplate> 
        </ItemsControl> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

該代碼的工作原理就顯示而言(它充滿零是一個默認值)。但我也想允許在Matrix屬性中反映的TextBoxes中的更改 - 現在TextBoxes不能更改,因爲它們的值綁定到我猜測的Matrix單元格的一種方式。我試着設置<TextBox Text="{Binding Mode=TwoWay}" />或者類似的東西,但它似乎不起作用。 任何想法應該如何限制數據?編輯: 我已經實施了INotifyPropertyChanged。 這裏是我的課的一部分:

public partial class CalcMatrix : INotifyPropertyChanged 
    { 
     public ObservableCollection<ObservableCollection<int>> Matrix 
     { 
      get { return _matrix; } 
      set 
      { 
       _matrix = value; 
       OnPropertyChanged("Matrix"); 
      } 
     } 
     private ObservableCollection<ObservableCollection<int>> _matrix; 

     private void OnPropertyChanged(string argName) 
     { 
      var handler = PropertyChanged; 
      if(handler != null) 
       handler(this, new PropertyChangedEventArgs(argName)); 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 
    } 

我覺得TexBoxes不改變的原因是因爲綁定是單向的 - 文本始終是什麼矩陣內。我相信我應該以某種方式將XAML綁定到TwoWay或其他東西,但不知道如何。有任何想法嗎 ?

+0

「公共的ObservableCollection <的ObservableCollection >矩陣」 ..其一個屬性,但它不是像字符串或整數其集合的原始數據類型。 –

回答

0

雙向結合模式需要路徑? see this SO answer),所以你不能像{Binding Mode=TwoWay}那樣做,它必須是類似於{Binding SomePath, Mode=TwoWay}的東西。因此,在這種情況下,您必須將矩陣項包裝爲某個類而不是純int,並將該int作爲該類的屬性值。

//your Matrix property type become: 
... 
public ObservableCollection<ObservableCollection<MatrixElement>> Matrix 
... 

//MatrixElement class is something like: 
public class MatrixElement : INotifyPropertyChanged 
{ 
    private int _value; 
    public int Value 
    { 
     get { return _value; } 
     set { 
       _value = value; 
       OnPropertyChanged("Value"); 
      } 
    } 

    .... 
} 

//then you can bind TextBox in two way mode 
... 
<ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <TextBox Text="{Binding Value, Mode=TwoWay}" /> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 
... 
1

它沒有工作的原因是itemsource是一個矩陣的列表,你沒有對列表做任何改變iteself就像添加或從列表中刪除,而是改變列表中存在的項目的屬性我假設您正在使用一個ObservableCollection .... 所以,你需要實現INotifyPropertyChanged接口告訴嘿,我改變了請及時更新自己的UI ......(爲什麼

class YourClass : INotifyPropertyChanged 
{ 

private string yourProperty; 
public string YourPropety{ 
    get{ 
     return yourProperty; 
    } 
    set{ 
     if (value != this.yourProperty) 
      { 
       this.yourProperty = value; 
       NotifyPropertyChanged(); 
      } 
    } 
}  

public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 
+0

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx –

+0

我已經實現了INotifyPropertyChanged。看看我編輯的帖子。 – wzielin3