0

I have seen a lot of questions and answers with almost the same problem, but none of these answers arent working for me. Soo, my code is:

<ListBox ItemsSource="{Binding Avakuvaandmed}" x:Name="lboxandmed" HorizontalAlignment="Left" Height="552" VerticalAlignment="Top" Width="970" SelectionChanged="lboxandmed_SelectionChanged" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" x:Name="spanVärviSeda">
    I HAVE TO GET VALUE OF THIS --> <TextBlock x:Name="IDbox" Width="50" Text="{Binding Id}"></TextBlock>
                                    <TextBlock Width="130" Text="{Binding Nrmärk}"></TextBlock>
                                    <TextBlock x:Name="txtKehtivus" Width="130" Text="{Binding Lõpp}"></TextBlock>
                                    <TextBlock Width="130" Text="{Binding Eesnimi}"></TextBlock>
                                    <TextBlock Width="130" Text="{Binding Perenimi}"></TextBlock>
                                    <TextBlock Width="130" Text="{Binding Mark}"></TextBlock>
                                    <TextBlock Width="130" Text="{Binding Mudel}"></TextBlock>
                                    <TextBlock Width="130" Text="{Binding Aasta}"></TextBlock>
                                </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

And I have to get the value of the textblock named "IDbox". Please can someone help me, or atleast give me a clue how.

Marek
  • 261
  • 4
  • 14

1 Answers1

0

Your code looks correct. If you want to access the Value of IDbox in code behind then you can do it by Avakuvaandmed.ElementAt(rowno).Id because you are binding Id to the IDBox. If you want to access the BoxId value in xaml. Then use binding as follows:

{Binding Avakuvaandmed[rowno],Path=Id}

You can also access Textblock value by using VisualTreeHelper class. You will need to go traverse all elements in ListBox.

techfun
  • 913
  • 2
  • 13
  • 25
  • i am sorry. I made typing mistake You will need to write Avakuvaandmed.ElementAt(rowno) – techfun Nov 26 '12 at 11:06
  • Still not working, maybe i have done something els worng, because im getting following error: The name 'Avakuvaandmed' does not exist in the current context – Marek Nov 26 '12 at 11:08
  • In xaml page, inside UserControl.Resources there should be collection Avakuvaandmed and also in code behind you will need to access the collection by lboxandmed.ItemSource. Just make sure your inside the proper scope the Avakuvaandmed exists. – techfun Nov 26 '12 at 11:10
  • What is the root element of xaml page? It is not necessary to to add the collection in Usercontrol.Resources. You can add the collection Avakuvaandmed in ListBox.Resources. And you can not bind Avakuvaandmed collection to anything in xaml directly if it is not defined in xaml page inside resorces. – techfun Nov 26 '12 at 11:19
  • The root element is window. The binding itself works, but i need to get the value of the textblock on the selected row. This is the first big project that im working on and im not very good at this. But i want to undrestand. – Marek Nov 26 '12 at 11:28
  • The error 'Avakuvaandmed' does not exist in the current context is strange in xaml.cs file. Make sure you are accessing the 'Avakuvaandmed' collection from window.resources['Avakuvaandmed'] in xaml.cs file. I am not sure about syntax because I dont have intellisence. I hope you are understanding what I want to convey. Once you get the collection you can get the value of textblock by Avakuvaandmed.ElementAt(rowno). – techfun Nov 26 '12 at 11:33
  • Are there any other ways how to get the value? VisualTreeHelper class didnt work either. I will keep trying... – Marek Nov 26 '12 at 12:46
  • If you can post the entire code, it would be easy to find the solution – techfun Nov 26 '12 at 13:43