Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new in WPF and trying it but the code part is more hard than WForm

my problem is i am search for how can i get textblock text from listboxitem in listbox

i tried more than one code but i cant get the text of the selected item from the list box

thanks

What I have tried:

<ListBox x:Name="listWithImg" Height="200" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="250 20 0 0" SelectionChanged="listWithImg_SelectionChanged">
    <ListBoxItem Height="50">
        <StackPanel Orientation="Horizontal">
            <Image Height="25" Width="25" Source="Image/C.png"/>
            <TextBlock Text="C Programming" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 0 0 0"/>
        </StackPanel>
    </ListBoxItem>
    <ListBoxItem Height="50">
        <StackPanel Orientation="Horizontal">
            <Image Height="25" Width="25" Source="Image/CPP.png"/>
            <TextBlock Text="C Plus Plus Programming" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 0 0 0"/>
        </StackPanel>
    </ListBoxItem>
    <ListBoxItem Height="50">
        <StackPanel Orientation="Horizontal">
            <Image Height="25" Width="25" Source="Image/CSharp.png"/>
            <TextBlock Text="C Sharp Programming" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 0 0 0"/>
        </StackPanel>
    </ListBoxItem>
    <ListBoxItem Height="50">
        <StackPanel Orientation="Horizontal">
            <Image Height="25" Width="25" Source="Image/Raspberry.png"/>
            <TextBlock Text="Raspberry" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 0 0 0"/>
        </StackPanel>
    </ListBoxItem>
</ListBox>

<TextBlock x:Name="txtblkSelectedItem" Text="Select To CHange" Height="30" Width="200" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="100 240 0 0" Padding="5" />



private void listWithImg_SelectionChanged(object sender, SelectionChangedEventArgs e)
{


    txtblkSelectedItem.Text = name;
}
Posted
Updated 13-Nov-19 21:08pm

1 solution

Hello,

you can try this:
txtblkSelectedItem.Text = ((((sender as ListBox).SelectedValue as ListBoxItem).Content as StackPanel).Children[1] as TextBlock).Text;

It should show your desired output. But you should consider DataBinding/Templates and create objects for your items. Then you can directly access the properties.
 
Share this answer
 
Comments
Eng Mohamed Bassuny 14-Nov-19 14:52pm    
this work perfect .. thanks

but i keep wonder why WPF Code is more Difficult than Windows form code ?!
Member1x 15-Nov-19 2:48am    
I guess that's a point of view. WPF and Forms are different. And what you are used to is always easier ;)
Both kind of code have their advantages.
Nemesis Rufius 4-Jul-20 16:15pm    
I have tried this but it will not work.

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