Declaring a string variable to a Textblock RRS feed

  • Question

  • I might be a bit stupid asking this question. But i have not found anything on the net as of yet with my problem. I have a windows app, a listview displaying Texblocks with names in them and is loaded from a xml file. Here is the xaml code.

    <ListView x:Name="listMyLoans" RenderTransformOrigin="0.446,0.54" Margin="10,150,1052,10" Grid.RowSpan="2">
                        <Grid Width="300" Height="100" >
                            <TextBlock x:Name="tbTitle" Text="{Binding Title}"></TextBlock >
                            <TextBlock x:Name="tbMediaIndex" Text="{Binding MediaIndex}" Visibility="Collapsed"/>

    and i need to declare it like to so in code behind. string strMediaIndex = tbMediaIndex.Text; Something like this but does not work, somehow does not see the TextBlock at all.


    Thursday, March 6, 2014 9:04 AM


All replies

  • no inded this isnt working because a listview is a list of items, so the TextBlock tbMediaIndex is created multiple times (for each list item one). you will need to use the VisualTreeHelper to get it out, or get the data from your viewmodel. ifyou have more info for what you try to do can provide better help. i cant detect now how you want to get the right textbox.

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Thursday, March 6, 2014 10:17 AM
  • Thanks for the reply.

    I am very new to Windows Store Apps, so how would go on doing this. Adding a VisualTreeHelper, to my listview and how i would be able to get the data from the viewmodel.


    Thursday, March 6, 2014 1:12 PM
  • Hi,

    Just like Dave indicate, a listview is a list of items, so the TextBlock tbMediaIndex is created multiple times (for each list item one). If you want to get a control in listview item, you should use VisualTreeHelper in listview SelectionChangedEvent or itemClick event, see the link below:


    If you only want to get the control's Value(like Text), you can get the listview item value in itemClick event and get the binding property of the item(which the property bind to the control Text Property).

    And I recommend you can refer to these links below to get more information about data binding and listView control:




    Best Wishes!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Edited by Anne Jing Monday, March 10, 2014 9:09 AM edit
    • Marked as answer by Anne Jing Monday, March 17, 2014 1:50 AM
    Monday, March 10, 2014 9:09 AM