在我的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或其他東西,但不知道如何。有任何想法嗎 ?
「公共的ObservableCollection <的ObservableCollection>矩陣」 ..其一個屬性,但它不是像字符串或整數其集合的原始數據類型。 –