2016-11-23 58 views
0

我寫在C#和WPF數獨的應用程序。到目前爲止我使用了我的文本框,但現在我想用陣列填充它([][])。綁定數組值到文本框

對於我的第一次測試中,我發現了一種方法來手動完成。 XAML:

<TextBox> Text="{Binding Path=Testarr[0][0]}" Name="testbox"></TextBox> 

,並在我的xaml.cs:

testbox.DataContext = this; 

所以現在我的文本框顯示這是在Testarr初始化值[0] [0]。

對於我的數獨,我有81個文本框,和我不想要手動初始化所​​有的人,有沒有辦法做到很簡單?

+0

是。 MVVM。 ItemsControl與ItemTemplate。但是這需要學習一些東西,所以只需編寫一個perl腳本來生成帶有綁定的所有XAML。 –

回答

1

你可以看一下here它顯示瞭如何使用ItemsControl來創建你的網格。

<ItemsControl ItemsSource="{Binding MyCollection}"> 
     <!-- ItemsPanelTemplate --> 
     <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
             <UniformGrid Columns="2" /> 
         </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
  
     <!-- ItemTemplate --> 
     <ItemsControl.ItemTemplate> 
         <DataTemplate> 
             <Button Content="{Binding }" /> 
         </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
+0

我也可以讀取這些單元格中的用戶輸入嗎? – NoIdea123

+0

使用TwoWay綁定,用戶輸入將自動設置爲匹配的視圖模型實例。 – Vincent