I have used wrappanel and listbox to display my items on wp7. but item click event does not working. my code is as follow
<Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto">
<ListBox x:Name="lstDevice">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel>
<Button x:Name="btnData" >
<StackPanel Orientation="Vertical">
<Canvas
Width="175"
Height="175"/>
<TextBlock Text="{Binding Name}" Width="175" />
</StackPanel>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
above is design code and c# code is below
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
lstDevice.ItemsSource = MainPage.user.dArray.ToList();
lstDevice.SelectionChanged += item_Select;
}
private void item_Select(object sender, SelectionChangedEventArgs e)
{
int p = ((ListBox)sender).SelectedIndex;
}
how to generate listbox item select event and get the number or some property to recognize which item is selected? Thanks in advance!
I think this may suits you better:
<Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto">
<ListBox x:Name="lstDevice">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate >
<Button x:Name="btnData" Click="OnButtonClick" Tag="{Binding Name}" >
<StackPanel Orientation="Vertical">
<Canvas
Width="175"
Height="175"/>
<TextBlock Text="{Binding Name}" Width="175" />
</StackPanel>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
lstDevice.ItemsSource = MainPage.user.dArray.ToList();
}
private void OnButtonClick(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
var nameInTag=b.Tag.ToString();
}