Čemu ti uopšte služi taj ListBox? Nema smisla da imaš Canvas u Listbox-u. Napravi Canvas u ScrollVieweru (a ne u listboxu), pa dodaj dugmice na Canvas iz koda, kao što izgleda već sada radiš.
Bolje rešenje je da definišeš ItemTemplate za ListBox tako da ItemTemplate pravi dugmiće:
Code:
<ListBox x:Name="ListaListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="3">
<Label Content="{Binding Caption}" />
<Button Content="{Binding Caption}" Click="Button_Click" Tag="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code (csharp):
public class ListItemModel
{
public string Caption
{ get
; set
; }
public int SomeData
{ get
; set
; }
}
public void Init
()
{
...
ListaListBox.ItemsSource = new List
<ListItemModel
> {new ListItemModel
{Caption
= "Test1", SomeData
= 1},
new ListItemModel
{Caption
= "Test2", SomeData
= 2}};
...
}
private void Button_Click
(object sender, RoutedEventArgs e
)
{
MessageBox
.Show(((sender
as Button
).Tag as ListItemModel
).SomeData.ToString());
}
if it walks like a duck and quacks like a duck, it could be a dragon doing a duck
impersonation.