Introducing XDA:DevCon – A Conference For Developers By Developers
XDA Developers Android and Mobile Development Forum
Forgot your password?
 
Post Reply+
Tip us?
 
GFR_2009
Old
#1  
Member - OP
Thanks Meter 0
Posts: 37
Join Date: Oct 2009
Default [Q] WP7 - ListBox w/template ADD Item??

Hi there,

Well, here is my situation and hopefully someone could shed some light on this annoying dilemma...

I'm pretty sure the solution is quite easy ,but so far I cannot find one.

I have a ListBox with a data template.

Now, I want to add some items (pretty easy, isn't it?

But...I always get the same data in every row...

Here is my code

Code:
Option Strict On
Option Explicit On

Partial Public Class MainPage
    Inherits PhoneApplicationPage

    Dim DataSelected As New Class_DS

    ' Constructor
    Public Sub New()
        InitializeComponent()
    End Sub

    Public Class Class_DS

        Public Property DS_Distance() As Integer
        Public Property DS_Value() As String
        Public Property DS_Angle() As String

    End Class

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        For x As Integer = 1 To 4
            DataSelected.DS_Distance = x
            DataSelected.DS_Value = (x + 2).ToString
            DataSelected.DS_Angle = (x + 3).ToString
            Me.ListBox_DS.Items.Add(DataSelected)
        Next x

    End Sub

End Class
Quite simple, but doesn't work as expected!

Just in case, here goes the XAML

Code:
        <DataTemplate x:Key="Template_DS">
            <StackPanel>
                <Border Width="450" CornerRadius="15">
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Black" Offset="0"/>
                            <GradientStop Color="#FF032E45" Offset="1"/>
                        </LinearGradientBrush>
                    </Border.Background>
                    <Grid>
                        <TextBlock x:Name="TextBlock_DS_Distance" Text="{Binding DS_Distance}" VerticalAlignment="Top" d:LayoutOverrides="Width" FontSize="26.667" Margin="4,15,4,0"/>
                        <TextBlock x:Name="TextBlock_DS_Value" Text="{Binding DS_Value}" Margin="4,50,4,0" d:LayoutOverrides="Width" FontSize="26.667" VerticalAlignment="Top" Foreground="#FFE6FF05"/>
                        <TextBlock x:Name="TextBlock_DS_Angle" Text="{Binding DS_Angle}" d:LayoutOverrides="Width" FontSize="21.333" Margin="4,85,4,0" VerticalAlignment="Top" Foreground="White"/>
                    </Grid>
                </Border>
            </StackPanel>
        </DataTemplate>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox x:Name="ListBox_DS" ItemTemplate="{StaticResource Template_DS}" Background="{x:Null}" Margin="3,111,3,0" BorderBrush="Blue" BorderThickness="8" Height="490" VerticalAlignment="Top" />
            <Button Content="Button" Height="87" HorizontalAlignment="Left" Margin="9,18,0,0" Name="Button1" VerticalAlignment="Top" Width="441" />
        </Grid>
What I get is 4 rows with exactly the same data...

Row 1

4
6
7

Row 2

4
6
7
....

What I expect is to get this

Row 1

1
3
4

Row 2

2
4
5


Don't have a clue where the issue is, perhaps with the class template or else...

Amybe I'm missing some sort of method??

Any help is greatly appreciated!
 
Ren13B
Old
#2  
Senior Member
Thanks Meter 19
Posts: 250
Join Date: Oct 2007
When databinding you need to set up INotifyPropertyChanged so the listbox will update when your observable collection changes.

The Windows Phone Databound Application template installed by default with the SDK will show you exactly how to do this properly.
 
GFR_2009
Old
#3  
Member - OP
Thanks Meter 0
Posts: 37
Join Date: Oct 2009
Quote:
Originally Posted by Ren13B View Post
When databinding you need to set up INotifyPropertyChanged so the listbox will update when your observable collection changes.

The Windows Phone Databound Application template installed by default with the SDK will show you exactly how to do this properly.
Ren13B,

Thanks for the answer! but I managed to solve the issue so far.

Best regards and Happy 2011!
 
Post Reply+
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Go to top of page...