Answered by:
Declaring a string variable to a Textblock

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">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="300" Height="100" >
<TextBlock x:Name="tbTitle" Text="{Binding Title}"></TextBlock >
<TextBlock x:Name="tbMediaIndex" Text="{Binding MediaIndex}" Visibility="Collapsed"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>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.
Thanks
Thursday, March 6, 2014 9:04 AM
Answers
-
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:
http://msdn.microsoft.com/en-us/library/windows/apps/hh464965.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/hh780650.aspx
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 "Mark as Answer" 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.Monday, March 10, 2014 9:09 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.
Thanks
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:
http://msdn.microsoft.com/en-us/library/windows/apps/hh464965.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/hh780650.aspx
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 "Mark as Answer" 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.Monday, March 10, 2014 9:09 AM