How do I update ItemTemplate after scrambling ObservableCollection(Of ObservableCollection(Of object
Posted
by user342195
on Stack Overflow
See other posts from Stack Overflow
or by user342195
Published on 2010-05-16T03:34:55Z
Indexed on
2010/05/16
3:40 UTC
Read the original article
Hit count: 490
I am learning vb.net, wpf and xaml with the help of sites like this one. The project I am currently working on is a 4 x 4 slide puzzle.
I cannot get the buttons in the grid to scramble to start a new game when calling a new game event. Any help will be greatly appreciated. If no answer is can be provide, a good resource to research would help as well.
Thank you for your time.
XAML:
<Window x:Class="SlidePuzzle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Slide Puzzle" Height="391" Width="300" Name="wdw_SlidePuzzle">
<Window.Resources>
<DataTemplate x:Key="DataTemp_PuzzleButtons">
<Button Content="{Binding C}"
Height="50"
Width="50"
Margin="2"
Visibility="{Binding V}"/>
</DataTemplate>
<DataTemplate x:Key="DataTemplate_PuzzleBoard">
<ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemp_PuzzleButtons}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
<Setter Property="Canvas.Left" Value="{Binding Path=X}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</DataTemplate>
</Window.Resources>
<DockPanel Name="dpanel_puzzle" LastChildFill="True">
<WrapPanel DockPanel.Dock="Bottom" Margin="5" HorizontalAlignment="Center">
<Button Name="bttnNewGame" Content="New Game" MinWidth="75" Margin="4" Click="NewGame_Click"></Button>
<Button Name="bttnSolveGame" Content="Solve" MinWidth="75" Margin="4"></Button>
<Button Name="bttnExitGame" Content="Exit" MinWidth="75" Margin="4" Click="ExitGame_Click"></Button>
</WrapPanel>
<WrapPanel DockPanel.Dock="Bottom" Margin="5" HorizontalAlignment="Center">
<Label>Score:</Label>
<TextBox Name="tb_Name" Width="50"></TextBox>
</WrapPanel>
<StackPanel Name="SlidePuzzlePnl" HorizontalAlignment="Center" VerticalAlignment="Center" Height="206" Width="206" >
<ItemsControl x:Name="lst" ItemTemplate="{DynamicResource DataTemplate_PuzzleBoard}"/>
</StackPanel>
</DockPanel>
VB:
Imports System.Collections.ObjectModel
Class SlidePuzzle
Dim puzzleColl As New ObservableCollection(Of ObservableCollection(Of SlidePuzzleBttn))
Dim puzzleArr(3, 3) As Integer
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
For i As Integer = 0 To 3
puzzleColl.Add(New ObservableCollection(Of SlidePuzzleBttn))
For j As Integer = 0 To 3
puzzleArr(i, j) = (i * 4) + (j + 1)
puzzleColl(i).Add(New SlidePuzzleBttn((i * 4) + (j + 1)))
puzzleColl(i)(j).X = j * 52
puzzleColl(i)(j).Y = i * 52
Next
Next
lst.ItemsSource = puzzleColl
End Sub
Private Sub NewGame_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim rnd As New Random
Dim ri, rj As Integer
Dim temp As Integer
For i As Integer = 0 To 3
For j As Integer = 0 To 3
ri = rnd.Next(0, 3)
rj = rnd.Next(0, 3)
temp = puzzleArr(ri, rj)
puzzleArr(ri, rj) = puzzleArr(i, j)
puzzleArr(i, j) = temp
puzzleColl(i)(j).X = j * 52
puzzleColl(i)(j).Y = i * 52
puzzleColl(i)(j).C = puzzleArr(i, j)
Next
Next
End Sub
End Class
Public Class SlidePuzzleBttn Inherits DependencyObject
Private _c As Integer
Private _x As Integer
Private _y As Integer
Private _v As String
Public Shared ReadOnly ContentProperty As DependencyProperty = DependencyProperty.RegisterAttached("_c", GetType(String), GetType(SlidePuzzleBttn), New UIPropertyMetadata(""))
Public Sub New()
_c = 0
_x = 0
_y = 0
_v = SetV(_c)
End Sub
Public Sub New(ByVal cVal As Integer)
_c = cVal
_x = 0
_y = 0
_v = SetV(cVal)
End Sub
Public Property C() As Integer
Get
Return _c
End Get
Set(ByVal value As Integer)
_c = value
End Set
End Property
Public Property X() As Integer
Get
Return _x
End Get
Set(ByVal value As Integer)
_x = value
End Set
End Property
Public Property Y() As Integer
Get
Return _y
End Get
Set(ByVal value As Integer)
_y = value
End Set
End Property
Public Property V() As String
Get
Return _v
End Get
Set(ByVal value As String)
_v = value
End Set
End Property
Private Function SetV(ByRef cVal As Integer) As String
If cVal = 16 Then
Return "Hidden"
Else
Return "Visible"
End If
End Function
End Class
© Stack Overflow or respective owner