Binding 1 DataSet with Relations to one Listbox?!? =)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello Folks,
I have a problem with my Databindings
Some Info before you read my code:
i am not connecting to a database with just reading xml files into my dataset/datatables
but in this code i have written the tables and rows in the code (for testing) dont have xml files yet
My Goal is it to have 1 DataSet with 2 DataTables and show Online Players and the Current Game they are Playing.
Both the Players Datatable and the Games Datatable will have an relation on the Game_ID
Then i would display that like an master - detail view
IMPORTANT: There will be an history later that shows what game the player is playing right now and what games he played before based on a daily view ....
Means I am playing Battlefiel 3 Right now and before i have played Call of Duty MW2 and Counter Strike 1,6 so Battlefield 3 Forderground is set to GREEN and COD and CS 1,6 to red (BUT THAT IS NOT IMPORTAND) its just the reason why i have a listbox with another
listbox inside :)
OK HERE IS WHAT I GOT SO FAR:
<pre class="prettyprint lang-vb Imports System.Data
Imports System.ComponentModel
Imports System.Collections.ObjectModel

Class MainWindow

Private dsMain As New DataSet

Public Sub New()

InitializeComponent()

Testing... create tables & rows & relations
createDataTables()
createRecords()
createRelations()

sets the BINDING!!!!
setListBoxBinding()

End Sub

<summary>
This Method creates 2 DataTables (for testing) and adds them in the DataSet (dsMain)
</summary>
<remarks></remarks>
Private Sub createDataTables()

Creating 2 new Datatables

Dim dtOnlinePlayers As New DataTable
Dim dtOnlineGames As New DataTable

With dtOnlinePlayers
.TableName = "Online_Players"
.Columns.Add("PlayerID", GetType(Integer)) = System Guid with {}
.Columns.Add("Player_Username", GetType(String)) = Player Nickname
.Columns.Add("GameID", GetType(String)) = System Guid with {}
End With

With dtOnlineGames
.TableName = "Online_Games"
.Columns.Add("GameID", GetType(String)) = System Guid with {}
.Columns.Add("GameName", GetType(String)) = Game Name
End With

Adding the 2 Datatables to my Dataset (dsMain)

With dsMain
.Tables.Add(dtOnlinePlayers)
.Tables.Add(dtOnlineGames)
End With

End Sub

<summary>
This Method inserts 2 test Rows to each DataTable
</summary>
<remarks></remarks>
Private Sub createRecords()

Creates a test guid for the later table relation
Dim gameGuid As String = createTestGuid()

Dim drPlayer As DataRow = dsMain.Tables("Online_Players").NewRow
Dim drGame As DataRow = dsMain.Tables("Online_Games").NewRow

With drPlayer
.Item("PlayerID") = 1 Player ID
.Item("Player_Username") = "Test Player" the screen name (nickname of the user)
.Item("GameID") = gameGuid the guid for the relation
End With

With drGame
.Item("GameID") = gameGuid the guid for the relation
.Item("GameName") = "Call of Duty MW2" The name of the Game associated with the Game ID
End With

adds the 2 datarows to their Table
dsMain.Tables("Online_Players").Rows.Add(drPlayer)
dsMain.Tables("Online_Games").Rows.Add(drGame)

End Sub

<summary>
Returns a System.Guid with "{}"
</summary>
<returns></returns>
<remarks></remarks>
Private Function createTestGuid() As String

Dim myGuid As String = "{" & Guid.NewGuid.ToString & "}"

Return myGuid

End Function

<summary>
Creates an Relation on the GameID for table Online_Players and Online_Games (i think)!!!!!!!!!!!
</summary>
<remarks></remarks>
Private Sub createRelations()

dsMain.Relations.Add("PlayerGameRelation", _
dsMain.Tables("Online_Players").Columns("GameID"), _
dsMain.Tables("Online_Games").Columns("GameID"), True)

End Sub

Private Sub setListBoxBinding()

lstbPlayerInfos.ItemsSource = dsMain.Tables("Online_Players").DefaultView

End Sub

End Class
[/code]
<br/>
and my XAML:

<pre class="prettyprint lang-vb <Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="660

<Grid>
<ListBox x:Name="lstbPlayerInfos" IsSynchronizedWithCurrentItem="True" Width="300" Height="250" Margin="0
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel x:Name="stpPlayerView" ClipToBounds="True" Orientation="Horizontal
<TextBlock x:Name="tBlockPlayerName" Width="110" FontWeight="Bold" Text="{Binding Player_Username}"/>
<ListBox x:Name="lstbGameInfo" Width="100" IsSynchronizedWithCurrentItem="True" >
<ListBox.ItemTemplate >
<DataTemplate >
<TextBlock x:Name="tBlockGameName" Width="110" FontWeight="Bold" Text="Here Should be the Game Name Displayed"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>

</Window>
[/code]
<br/>
MY BIGGEST PROBLEM IS TO DISPLAY THE BINDING WITH THE RELATIONS

IF SOMEBODY HAS SOME HINTS FOR ME ON THAT I WOULD REALLY BE HAPPY!


View the full article
 
Back
Top