Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I bind a simple array?
    primarykey
    data
    text
    <p>I'm making a scrabble-like game in WPF C#. As of current I've got the AI Algorithm working with operates on a "Model Game Board", a variable string[15,15]. However over the past few days I'm stuck at producing a GUI to display this Array as a GameBoard.</p> <p>As of current I've got the following XAML Code:</p> <p>My "MainWindow" which contains:</p> <p>A button:</p> <pre><code>&lt;Button Click="Next_TurnClicked" Name="btnNext_Turn"&gt;Next Turn&lt;/Button&gt; </code></pre> <p>A UserControl: Which is the Game Board(GameBoard is also another UserControl) and the Player's Rack</p> <pre><code>&lt;clr:Main_Control&gt;&lt;/clr:Main_Control&gt; </code></pre> <p>Then Inside my UserControl I have:</p> <pre><code> &lt;DockPanel Style ="{StaticResource GradientPanel}"&gt; &lt;Border Style ="{StaticResource ControlBorder}" DockPanel.Dock="Bottom"&gt; &lt;ItemsControl VerticalAlignment="Center" HorizontalAlignment="Center"&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel Height ="Auto" Name="ListBox" Orientation="Horizontal" HorizontalAlignment="Center"&gt; &lt;/StackPanel&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;clr:Rack_Cell_Sender x:Name="Player_Tile_1" &gt;&lt;/clr:Rack_Cell_Sender&gt; &lt;clr:Rack_Cell_Sender x:Name="Player_Tile_2" &gt;&lt;/clr:Rack_Cell_Sender&gt; &lt;clr:Rack_Cell_Sender x:Name="Player_Tile_3" &gt;&lt;/clr:Rack_Cell_Sender&gt; &lt;clr:Rack_Cell_Sender x:Name="Player_Tile_4" &gt;&lt;/clr:Rack_Cell_Sender&gt; &lt;clr:Rack_Cell_Sender x:Name="Player_Tile_5" &gt;&lt;/clr:Rack_Cell_Sender&gt; &lt;/ItemsControl&gt; &lt;/Border&gt; &lt;Viewbox Stretch="Uniform"&gt; &lt;Border Margin="5" Padding="10" Background="#77FFFFFF" BorderBrush="DimGray" BorderThickness="3"&gt; &lt;Border BorderThickness="0.5" BorderBrush="Black"&gt; &lt;clr:GameBoard&gt; &lt;/clr:GameBoard&gt; &lt;/Border&gt; &lt;/Border&gt; &lt;/Viewbox&gt; &lt;/DockPanel&gt; </code></pre> <p>clr:GameBoard is a ItemsControl with its ItemsPanelTemplate as a UniformGrid</p> <pre><code> &lt;Style TargetType="ItemsControl"&gt; &lt;Setter Property="ItemsPanel"&gt; &lt;Setter.Value&gt; &lt;ItemsPanelTemplate&gt; &lt;UniformGrid IsItemsHost="True" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Rows="15" Columns="15" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Setter Property="ItemTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;Words:Cell&gt; &lt;/Words:Cell&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; &lt;ItemsControl Name="BoardControl" ItemsSource="{DynamicResource CellCollectionData}"&gt; &lt;/ItemsControl&gt; </code></pre> <p><img src="https://i.stack.imgur.com/jtkdo.png" alt="UI Structure"></p> <p>So my question is:</p> <ol> <li>How do I get my array[,] to the ItemsControl? I have no ideas how to DataBind, I've read a couple of tutorials but I'm only starting to get what a DataContext is. </li> <li>How will I refresh the Board after each turn? I don't want the board updating before btnNext_Turn is clicked.</li> <li>How do I Update the ModelBoard after user inputs new word into UI and btn_Next_Turn is clicked?</li> </ol> <p>I'm a beginner in coding and this is my first real project in C# and WPF. My teacher knows neither WPF or C# so you guys on the StackoverFlow community has been a great help over the past few weeks, especially helping me out on SQL.</p> <p>Please, any help will be much appreciated!</p> <p>UPDATE:</p> <p>Thanks Erno for the quick response! Yea, I got an error for EventHandler so I swapped it out for PropertyChangedEventHandler and that stopped the errors. </p> <pre><code>public partial class GameBoard : UserControl { TileCollection RefreshTiles = new TileCollection(); public GameBoard() { InitializeComponent(); } public void getBoard() { string[,] ArrayToAdd = InnerModel.ModelBoard; for (int i = 0; i &lt; 15; i++) { for (int j = 0; j &lt; 15; j++) { Tile AddTile = new Tile(); AddTile.Charater = ArrayToAdd[i, j]; AddTile.X = i; AddTile.Y = j; RefreshTiles.Add(AddTile); } } } } </code></pre> <p>So when I run debug I can see the RefreshTiles Collection being filled. However how do I bind the Collection to the ItemsControl? </p> <p>Do I set the DataContext of the UserControl to RefreshTiles?</p> <pre><code>this.DataContext = RefreshTiles </code></pre> <p>then in XAML</p> <pre><code>&lt;ItemsControl Name ="BoardControl" ItemsSource="{Binding}"&gt; </code></pre> <p>Update:</p> <p>So apparently if I set the bind up in the MainWindow it works perfectly however this does not work when i try binding from a Usercontrol? I set a breakpoint in "RefreshArray" and I can see it being populated, however the UserControl does not update?</p> <pre><code>public partial class UserControl1 : UserControl { public char GetIteration { get { return MainWindow.Iteration; } set { MainWindow.Iteration = value; } } CellCollection NewCells = new CellCollection(); public UserControl1() { InitializeComponent(); this.DataContext = NewCells; PopulateCells(); } private void PopulateCells() { for (int i = 0; i &lt; 15; i++) { for (int j = 0; j &lt; 15; j++) { Cell NewCell = new Cell(); NewCell.Character = "A"; NewCell.Pos_x = i; NewCell.Pos_y = j; NewCells.Add(NewCell); } } } public void RefreshArray() { NewCells.Clear(); for (int i = 0; i &lt; 15; i++) { for (int j = 0; j &lt; 15; j++) { Cell ReCell = new Cell(); ReCell.Character = GetIteration.ToString(); ReCell.Pos_x = i; ReCell.Pos_y = j; NewCells.Add(ReCell); } } this.DataContext = NewCells; } } public partial class MainWindow : Window { UserControl1 Control = new UserControl1(); public static char Iteration = new char(); public MainWindow() { InitializeComponent(); } private void Next_Click(object sender, RoutedEventArgs e) { Iteration = 'B'; Control.RefreshArray(); } } </code></pre> <p>This doesn't work while the one below does work</p> <pre><code>public partial class MainWindow : Window { char Iteration = new char(); CellCollection NewCells = new CellCollection(); public MainWindow() { InitializeComponent(); PopulateCells(); this.DataContext = NewCells; Iteration++; } private void PopulateCells() { for (int i = 0; i &lt; 15; i++) { for (int j = 0; j &lt; 15; j++) { Cell NewCell = new Cell(); NewCell.Character = "A"; NewCell.Pos_x = i; NewCell.Pos_y = j; NewCells.Add(NewCell); } } } private void RefreshArray() { NewCells.Clear(); for (int i = 0; i &lt; 15; i++) { for (int j = 0; j &lt; 15; j++) { Cell ReCell = new Cell(); ReCell.Character = Iteration; ReCell.Pos_x = i; ReCell.Pos_y = j; NewCells.Add(ReCell); } } } private void Next_Click(object sender, RoutedEventArgs e) { RefreshArray(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload