locked
How to get datatemplate values for selected listitem of listbox in code behind RRS feed

  • Question

  • Friends,

    I'm displaying Listbox with the following values in WPF Application,
     1. Image ( in image control ),
     2. Event Description ( in TextBlock ) and
     3. Event Id ( In Label which is hidden ) 

    I have done this on Datatemplate. Now I want to get respective Event Id when user selects items in listbox in code behind ( SelectionChanged event ).

    I have pasted my code below,

    <Window.Resources>

            <local:ImageConverter x:Key="ImageConverter"/>

            <DataTemplate x:Key="lstEventTemplate" x:Name="lstEventTemplate">

                <StackPanel x:Name="spEventTemplate" Background="Transparent" Margin="3">

                    <DockPanel Background="Transparent">

                        <Image Source="{Binding Path=Event_Img,Converter={StaticResource ImageConverter}}" Height="50" Width="50" DockPanel.Dock="Left"/>

                        <TextBlock Background="Transparent"  Text="{Binding Event_ShortMsg}" DockPanel.Dock="Right" Margin="20">

                            <TextBlock.Foreground>

                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,0">

                                    <GradientStop Color="White" Offset="0.0"/>

                                    <GradientStop Color="Blue" Offset="0.2"/>

                                </LinearGradientBrush>

                            </TextBlock.Foreground>

                        </TextBlock>

                        <Label x:Name="lblEventId" Visibility="Hidden" Content="{Binding Event_Id}"></Label>

                    </DockPanel>

                </StackPanel>

            </DataTemplate>

    </Window.Resources>

    <Canvas>

                        <ListBox x:Name="lstEvents" ItemsSource="{Binding}" ItemTemplate="{StaticResource lstEventTemplate}" SelectionChanged="lstEvents_SelectionChanged" />

                    </Canvas>

    How can i get selected Event Id from Label inside Listbox SelectionChanged event in code behind ?

    please guide me if anyone has idea.


    Thanks - Ravi


    • Edited by Ravi_P Thursday, July 26, 2012 9:32 AM
    Thursday, July 26, 2012 9:30 AM

Answers

  • Hi Nair & Pritesh,

       Thank you very much for ur valuable answering.  I got an output with other logic.

    private void lstEvents_SelectionChanged(object sender, SelectionChangedEventArgs e)

            {

                ListBoxItem lbi = (lstEvents.ItemContainerGenerator.ContainerFromIndex(lstEvents.SelectedIndex)) as ListBoxItem;

                ContentPresenter cp = GetFrameworkElementByName<ContentPresenter>(lbi);

                DataTemplate dt = lstEvents.ItemTemplate;

                Label l = (dt.FindName("lblEventId", cp)) as Label;

                MessageBox.Show(l.Content.ToString());   

            }


    Thanks - Ravi

    • Marked as answer by Ravi_P Thursday, July 26, 2012 11:03 AM
    Thursday, July 26, 2012 10:43 AM

All replies

  • This  may be of your help.

    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nair S

    Thursday, July 26, 2012 9:50 AM
  • Hi Ravi,

    An easy way to do this is to create a public property in your code behind and set the selected item proeprty of the listbox to that property. I don't know what your listbox is binding to. so lets say its a list containing objects of type Events.

    In your code behind create a new SelectedEvent's proeprty like so -

    private Events _selectedEvent

    public Events SelectedEvent

    {

    get

    {

    return _selectedEvent;

    }

    set

    {

    _seelctedEvent = value;

    }

    }

    now change your listbox xaml to bind the listbox's SelectedItem property to this property -

     <ListBox x:Name="lstEvents" ItemsSource="{Binding}" SelectedItem="{Binding SelectedEvent}" ItemTemplate="{StaticResource lstEventTemplate}" SelectionChanged="lstEvents_SelectionChanged" />

    Now if you put a breakpoint in the setter for the property you will see each time you select a new value from the listbox the breakpoint will "be hit". So instead of using selection changed event you just use that property to get the Eventid etc.

    Does that make sense?   

    Thursday, July 26, 2012 9:57 AM
  • Hi Nair & Pritesh,

       Thank you very much for ur valuable answering.  I got an output with other logic.

    private void lstEvents_SelectionChanged(object sender, SelectionChangedEventArgs e)

            {

                ListBoxItem lbi = (lstEvents.ItemContainerGenerator.ContainerFromIndex(lstEvents.SelectedIndex)) as ListBoxItem;

                ContentPresenter cp = GetFrameworkElementByName<ContentPresenter>(lbi);

                DataTemplate dt = lstEvents.ItemTemplate;

                Label l = (dt.FindName("lblEventId", cp)) as Label;

                MessageBox.Show(l.Content.ToString());   

            }


    Thanks - Ravi

    • Marked as answer by Ravi_P Thursday, July 26, 2012 11:03 AM
    Thursday, July 26, 2012 10:43 AM