Im completely new to developing silverlight and windows 7 phones. And I'm not sure what I missed, but provided that I missed something because it is not working properly.
My goal is to show a list of creatures, only with their name and hitpoints. But the entire Text = {Binding} material does not seem to work. So I wonder if any of you can help me with this.
When I say that it does not work, it is because the data is in the list of creatures, but not on the page / text blocks - it shows the correct number of creatures, but just not the data.
Xaml
<phone:PhoneApplicationPage x:Class="RPG_Assistent.Pages.DamageTrackerPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/> </StackPanel> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <ListBox ItemsSource="{Binding creatureList}" Height="500" HorizontalAlignment="Center" Margin="6,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="400"> <ListBox.ItemTemplate> <DataTemplate> <Button Width="400" Height="120" > <Button.ContentTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Height="80" Width="200"> <StackPanel Orientation="Vertical" Height="40"> <TextBlock Width="100" FontSize="22" Text="Name:" Height="40"/> <TextBlock Width="100" Text="{Binding Name}" Height="40"/> </StackPanel> <StackPanel Orientation="Vertical" Height="40"> <TextBlock Width="100" FontSize="22" Text="Hitpoints:" Height="40"/> <TextBlock Width="100" Text="{Binding HitPoints}" Height="40"/> </StackPanel> </StackPanel> </DataTemplate> </Button.ContentTemplate> </Button> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Grid>
CS - called when the page is loaded into a file. (called after InitializeComponent (), on my DamageTracker page)
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; namespace RPG_Assistent.Pages { public partial class DamageTrackerPage : PhoneApplicationPage { List<Models.Creature> creatureList { get; set; } public DamageTrackerPage() { InitializeComponent(); creatureList = new List<Models.Creature>(); #region ApplicationTitle Setup ApplicationTitle.Text = Constants.AppName; ApplicationTitle.TextAlignment = Constants.AppName_TextAlignment; ApplicationTitle.FontSize = Constants.AppName_FontSize; ApplicationTitle.FontWeight = Constants.AppName_FontWeight; #endregion //SetInputScope(txtDamage); LoadCreatures(); DataContext = this; } public void LoadCreatures() { string name; for (int i = 0; i < 10; i++) { name = "Monster " + i + 1; creatureList.Add(new Models.Creature(name)); } } public void btnDamage_Click(object sender, RoutedEventArgs e) { } #region textbox control - makes numeric only private void SetInputScope(TextBox textBoxControl) { InputScopeNameValue digitsInputNameValue = InputScopeNameValue.TelephoneNumber; textBoxControl.InputScope = new InputScope() { Names = { new InputScopeName() { NameValue = digitsInputNameValue } } }; } private void MaskNumericInput(TextBox textBoxControl) { string[] invalidCharacters = { "*", "#", ",", "(", ")", "x", "-", "+", " ", "@", "." }; for (int i = 0; i < invalidCharacters.Length; i++) { textBoxControl.SelectionStart = textBoxControl.Text.Length; } } private void NumericOnlyTextBox_KeyUp(object sender, KeyEventArgs e) { MaskNumericInput((TextBox)sender); } #endregion } }
CS - a creature class, placed in the Models folder - because I thought I would be smart
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace RPG_Assistent.Models { public class Creature { public string Name { get; set; } public int HitPoints { get; set; } public string Type { get; set; } public Creature(string name) { this.Name = name; this.HitPoints = 0; this.Type = "Images/mob.jpg"; } public void Damage(int damage) { HitPoints += damage; } public void Bloodied() { switch (this.Type) { case "Images/mob.jpg": this.Type = "Images/mobhurt.jpg"; break; case "Images/mobhurt.jpg": this.Type = "Images/mob.jpg"; break; } } } }