csharp get value datatemplate element

Posted by To-me on Stack Overflow See other posts from Stack Overflow or by To-me
Published on 2010-04-21T16:41:03Z Indexed on 2010/04/21 16:43 UTC
Read the original article Hit count: 156

Filed under:

Hello,

Here is my code

<ListBox x:Name="myList" IsSynchronizedWithCurrentItem="True" SelectionChanged="editElement">
                        <ListBox.ItemTemplate>
                            <DataTemplate x:Name="ElementItemTemplate">
                                <StackPanel Name="stackPanelElementItem" Orientation="Horizontal">
                                    <Label Name="SelectedItemlabel" Content="{Binding}" />
                                    <Button Name="buttonDelElement" Click="btnDelElement">Delete</Button>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

private void btnDelElement(object sender, RoutedEventArgs e)
        {
            ListBoxItem lbi2 = (ListBoxItem)(lstCursus.ItemContainerGenerator.ContainerFromItem(myList.Items.CurrentItem));
            String selectedItem = lbi2.Content.ToString();
MessageBox.Show("Selected Item " + selectedItem + " .");

private void editCursus(object sender, RoutedEventArgs e)
{
MessageBox.Show("Selected Item " + selectedItem + " .");
/* some code to edit selected item using linq */
}

My issue, SelectionChange doesn't work anymore and when I click on buttonDelElement, Selected Item doesn't change immediately.

Please, any ideas?

© Stack Overflow or respective owner

Related posts about c#