Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello,
I am trying to create a listbox with listbox items which contains some textboxes and buttons within it.
My motive is to catch the value of the selected listbox item on the click of those buttons within it.
here below is my xaml code

XML
<ListBox  HorizontalAlignment="Center" VerticalAlignment="Top" Width="700"
Padding="0,0,0,0" Height="400" Margin="0,0,0,50"  Name="dgDisplay">
                       <ListBox.ItemTemplate>
                           <DataTemplate>
                                       <Border Margin="10"  CornerRadius="6" Height="Auto" Width="Auto" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
               BorderBrush="Gray"
               BorderThickness="2"
               Padding="8">

                                           <StackPanel removed="#FF323232" >

                                               <WrapPanel>
                                                   <TextBlock Foreground="{StaticResource Foreground}" FontSize="30" Text="{Binding Account_Code}" Name="txtAcccode"/>
                                                   <TextBlock Foreground="{StaticResource Foreground}" FontSize="30" Text="{Binding Description}"/>
                                               </WrapPanel>
                                               <WrapPanel>
                                                   <TextBlock Foreground="{StaticResource Foreground}" FontSize="16" Text="{Binding GRP_CD}"/>
                                                   <TextBlock Foreground="{StaticResource Foreground}" FontSize="16" Text="{Binding Group}"/>
                                                   <TextBlock Foreground="{StaticResource Foreground}" FontSize="16" Text="{Binding Activate}" Visibility="Hidden"/>
                                               </WrapPanel>
                                               <WrapPanel>
                                                   <Button Content="Change" Click="ActivateGroupButton_Click" Width="80"  />
                                                   <Button Content="Edit" Click="EditGroupButton_Click" Width="80"  />
                                               </WrapPanel>

                                           </StackPanel>

                                       </Border>

                           </DataTemplate>
                       </ListBox.ItemTemplate>
                   </ListBox>





Below is the C# backend code for the one the button click to catch the seleted value i.e account code from the textblock txtAcccode
C#
private void ActivateGroupButton_Click(object sender, RoutedEventArgs e)
       {
           try
           {
              // object item = dgDisplay.SelectedItem;
               object item = dgDisplay.Items[0].ToString(); ;
               ListBoxItem lbi = (dgDisplay.ItemContainerGenerator.ContainerFromIndex(dgDisplay.SelectedIndex)) as ListBoxItem;
               ContentPresenter cp = GetFrameworkElementByName<ContentPresenter>(lbi);
               DataTemplate dt = dgDisplay.ItemTemplate;
               TextBlock l = (dt.FindName("txtAcccode", cp)) as TextBlock;
              // MessageBox.Show(l.Content.ToString());
               string ID = l.Text.ToString();
               Account_Master Account = (from o in db.Account_Masters where o.Account_Code.Trim() == ID.Trim() select o).Single();
               if (tgActivate.IsChecked == true)
               {
                   Account.Activate = 'I';
               }
               else
               {
                   Account.Activate = 'A';
               }
               Account.Edit_By = GlobalVariable.User;
               Account.Edit_Time = System.DateTime.Today;
               tbAccountCode.Text = ID.Trim();
               db.SubmitChanges();
               ((MainWindow)Application.Current.MainWindow).ShowMessageAsync("Success", "Row Updated Successfully.", MessageDialogStyle.Affirmative, null);
               dgDisplayBind();
               MainTab.SelectedIndex = 0;

           }
           catch (Exception Ex)
           {
               MessageBox.Show(Ex.Message);
               return;
           }
       }


So now the trouble is on button click the select index for the selected item in the listbox it gets is '-1'

Any help on this would be appreciated .
Thank you
Posted

1 solution

You are not using the listbox in a correct way.

You can do the following:
Let the Listbox reflect the state of your model.
In that case your model class should have a Selected property.
look at: Basic-MVVM-Listbox-Binding-In-WPF[^]

or

Make a usercontrol.
The usercontrol holds your data and the Xaml.
Add the usercontrol to the listboxItems.
then you can use

C#
private void ActivateGroupButton_Click(object sender, RoutedEventArgs e)
{
   Usercontrol(((Button)Sender).Parent) to have access to your data and controls.
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900