0

I'm working on a UWP application and need some help with Flyout. I have a Flyout in my XAML with a few TextBlock elements but don't seem to be able to call those elements in the Code Behind. Everytime I try that, I get an Exception "The name TB does not exist in the current context."

I already searched for possible solutions and tried the following things: Made a clean build, Restarted VS 2017, Cleared the bin folder manually and then tried to rebuild

But nothing seems to work and I'm on a point where I don't know what to do.

<Page
    x:Class="FuhrparkUWP.Pages.Parkhaeuser"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converter="using:FuhrparkUWP.Converter"
    xmlns:data="using:FuhrparkStructureUWP.Model"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>
        <converter:PassendFuerToStringConverter x:Key="PassendFuerToStringConverterKey"></converter:PassendFuerToStringConverter>
        <converter:BelegtStatusToImageConverter x:Key="BelegtStatusToImageConverterKey"></converter:BelegtStatusToImageConverter>
    </Page.Resources>

    <Grid>
        <ComboBox Name="CmbSelectParkhaus" Header="Parkhaus" HorizontalAlignment="Center" VerticalAlignment="Top" Width="200" SelectionChanged="CmbSelectParkhaus_SelectionChanged"/>

        <GridView ItemsSource="{x:Bind Parkplaetze}" Name="ContentGrid" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0, 100, 0, 0">
            <GridView.ItemTemplate>
                <DataTemplate x:Name="ImageTextDataTemplate" x:DataType="data:Parkplatz">
                    <StackPanel Height="280" Width="180" Margin="12" Tapped="Content_Tapped">
                        <Image Source="{Binding Path=IstBelegt, Converter={StaticResource BelegtStatusToImageConverterKey}}" Height="180" Width="180" Stretch="UniformToFill"/>
                        <StackPanel Margin="0,12">
                            <TextBlock Text="{x:Bind FahrzeugKennzeichen}"/>
                            <TextBlock Text="{Binding Path=PassendFuer, Converter={StaticResource PassendFuerToStringConverterKey}}" Style="{ThemeResource CaptionTextBlockStyle}" Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}"/>
                        </StackPanel>
                        <FlyoutBase.AttachedFlyout>
                            <Flyout>
                                <StackPanel>
                                    <Image Source="/Assets/Images/ParkplatzFrei.png" Width="180" Height="180"></Image>
                                    <TextBlock Name="TB"></TextBlock>
                                    <TextBlock Text="Passend für: LKW, PKW, Motorrad"></TextBlock>
                                    <TextBlock Text="Belegt durch: FREI"></TextBlock>
                                </StackPanel>
                            </Flyout>
                        </FlyoutBase.AttachedFlyout>
                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>
    </Grid>
</Page>

I expect to call TB.Text = "xyz" for example from the Code Behind page, but at the moment I'm not able to call it.

I'm still able to call the other elements outside from the Flyout like "CmbSelectParkhaus" though.

Capt.Pie
  • 108
  • 1
  • 2
  • 6
  • Possible duplicate of [How to find control from datatemplate of tabitem wpf](https://stackoverflow.com/questions/44492970/how-to-find-control-from-datatemplate-of-tabitem-wpf) –  Jan 14 '19 at 01:57

1 Answers1

1

You cannot handle things inside a DataTamplate, datamplates are not actual ui entities, they are Templates.

Assuming you want to achieve the same kind of access across all of your GridView items, you have to attach dependency properties, and either create programmatic binds on the PrepareContainerForItemOverride or the appropriate xaml "{Binding}" expressions.

This just opens whole new rabbit hole, especially if you are unaware of at least a single thing i mentioned so far, but you can just look up the bolded words one by one.

Xeorge Xeorge
  • 452
  • 4
  • 10
  • ok then put your flyout on and show it from code like Myflyout.ShowAt(some element) – Xeorge Xeorge Jan 13 '19 at 21:31
  • i did not see the entire thing was inside a datatamplate, you will have to use a dependency property to achieve wide bind targeting – Xeorge Xeorge Jan 13 '19 at 21:34
  • Moving it into and removing the FlyoutBase.AttachedFlyout did it! I'm now able to call the elements inside it! Thanks! Do you know the cause for the problem? – Capt.Pie Jan 13 '19 at 21:39
  • i edited it my original answer explaining why what you where trying didnt work. – Xeorge Xeorge Jan 13 '19 at 21:43
  • Thanks, at least the first thing makes sense to me if I think about it. But I'll look up the other things you mentioned. Here for learning. – Capt.Pie Jan 13 '19 at 21:46
  • _”You cannot handle things inside a DataTamplate, datamplates are not actual ui entities”_ - incorrect. You can _query_ for an element by searching the visual tree for a particular type at _runtime_ when data templates are instantiated. https://stackoverflow.com/questions/974598/find-all-controls-in-wpf-window-by-type –  Jan 14 '19 at 01:53
  • ...or https://stackoverflow.com/questions/44492970/how-to-find-control-from-datatemplate-of-tabitem-wpf –  Jan 14 '19 at 01:58