0
votes

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.

1
The DataTemplate isn't as "superfluous" as you imagined. The DataTemplate may be instantiated zero times, once, or a thousand times. Which of those zero or more instances of dateChosen did 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
If you're using templates, you should be doing this MVVM style, not winforms-event-handler style (which works very poorly with XAML at best). - 15ee8f99-57ff-4f92-890c-b56153
I had been trying to get the selection to be visible before adding/saving the selection throughout the rest of my 'Menu' class, but based on what @EdPlunkett said it seems I need to place it in my menu class before I can reliably display it. - TS-
Until I see your code, you might as well be speaking idiomatic Tocharian. There isn't a single clause in that comment that conveys the slightest information to me (no, wait, there's one: "reliably display it" hints that you might be in a locked ward). You could be hitting random keys otherwise. - 15ee8f99-57ff-4f92-890c-b56153

1 Answers

3
votes

This question comes up from time to time. Look here: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

Since the control is in a generated container, it's actually no longer in scope. That's why you can't reference it. You will have to ask the ListView for the container, and then ask the container for the element. That blog article will explain the steps. It's not all that difficult.

Think about this, by the way, if you had 10 items in your ListView, wouldn't you have 10 items with that same name? You can't do that, of course. And, that's sort of why this problem exists. The ListView has to constrain the scope of the child item so it doesn't violate simple OOP.