简体   繁体   中英

Element inside Flyout in XAML isn't accessible from Code Behind

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM