I want to update some text on a flyout. However, when I try to set its value in the code behind I get the error "The name 'dateChosen' does not exist in the current context".
Code behind:
private void updateDateBinding()
{
lstMenus.Visibility = Visibility.Visible;
dateChosen.Text = ""; // <-- Error here
}
Xaml code:
<ListView x:Name="lstMenus">
<!-- ...other code... -->
<TextBox x:Name="dateChosen" IsReadOnly="True"
Text="{Binding getDateChosen, Mode=TwoWay}"/>
<!-- ...other code... -->
</ListView>
Originally I was only going to use the PropertyChanged event to update 'dateChosen' but it did not update when calling that event. So I've decided to set the value explicitly instead, only to find this issue.
What confuses me is that 'lstMenus' can be altered exactly as I would expect. How can my ListView exist, but not the TextBox inside of it?
-
PS: I've cut most of superfluous code from this (Height/Width, Background, Alignment, etcetera) but I feel I should mention that the TextBox in question is pretty deeply nested in this chunk of code. The full inheritance is:
ListView(lstMenus) - ListView.ItemTemplate - DataTemplate - StackPanel - Grid - Grid - AppBarButton - AppBarButton.Flyout - Flyout - Grid - Grid - TextBox(dateChosen)
I've tried setting the 'x:Name' property for some of the intervening tags but always get the same "not in current context" issue when I try reaching them from the code behind.
DataTemplateisn't as "superfluous" as you imagined. TheDataTemplatemay be instantiated zero times, once, or a thousand times. Which of those zero or more instances ofdateChosendid you expect to be interacting with, and how did you expect the framework to guess the answer to that question? - 15ee8f99-57ff-4f92-890c-b56153