简体   繁体   English

后台代码找不到XAML中声明的控件

[英]Code-behind can't find control declared in XAML

After googling for sometime, x:Name should be a solution to my problem. 谷歌搜索一段时间后, x:Name应该可以解决我的问题。

In my wtfapp.xaml there is a TextBlock that will be created during runtime: 在我的wtfapp.xaml中,有一个TextBlock将在运行时创建:

<TextBlock x:Name="wtf" Text="{Binding fTx}"/>

In the code-behind, it should be accessible so I tried to change its Foreground color: 在后面的代码中,应该可以访问它,因此我尝试更改其前景颜色:

wtf.Foreground = Brushes.DarkGreen;

When I compile, an error shows up: 编译时出现错误:

The name "wtf" does not exist in the current context. 名称“ wtf”在当前上下文中不存在。

If I'm not mistaken, that means TextBlock "wtf" is not accessible. 如果我没记错的话,这意味着TextBlock “ wtf”是不可访问的。

How can I resolve the reference? 如何解决参考?

EDIT: 编辑:

XAML: XAML:

    <DataTemplate x:Key="ItemTemplate_NextAnime_HOVER">
        <Grid Margin="2,0,1,0" Width="82" Height="120" >
            <Image x:Name="NxtAnime_Image" Width="82" Height="120" Stretch="UniformToFill" Panel.ZIndex="0">
                <Image.Source>
                    <BitmapImage UriCachePolicy="Revalidate" UriSource="{Binding rLocalPic}"/>
                </Image.Source>
            </Image>
            <Grid Panel.ZIndex="1" >
                <Border Background="#7F000000" Panel.ZIndex="0" x:Name="brd">
                    <Popup IsOpen="True" StaysOpen="True" PlacementTarget="{Binding ElementName=brd}" Placement="Top">
                        <StackPanel Background="#FFC54B4B" Orientation="Horizontal" Height="334" Width="430">
                            <Image Width="213" Height="326" Stretch="UniformToFill" Margin="4,4,4,6" Source="{Binding LocalPic}"/>
                            <StackPanel Orientation="Vertical" Margin="4,4,4,4" Name="are">
                                <TextBlock Margin="0,0,0,10" Text="{Binding Title}" FontSize="22" Foreground="White" TextWrapping="Wrap" MaxWidth="200" FontWeight="Bold"/>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Relation: " FontSize="20" Foreground="White"/>
                                    <TextBlock x:Name="wtf" Text="{Binding Type}" FontSize="20" Foreground="White"/>
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                    </Popup>
                </Border>
            </Grid>
        </Grid>
    </DataTemplate>

This is a DataTemplate for ListBoxItem . 这是ListBoxItemDataTemplate

It's difficult to get an element from a template, as a template is like a factory, and will generate multiple instances. 从模板中获取元素非常困难,因为模板就像工厂一样,并且会生成多个实例。

As suggested by user2946329 in the comments, ListBox items return string, when DataTemplate is Button answers how to get the element, but there might be another way to do what you want. 正如user2946329在注释中所建议的那样,当DataTemplate为Button回答如何获取元素ListBox项目返回字符串 ,但是可能还有另一种方法来执行所需的操作。

For example, if you want to change the color of the wtf element you can do so with a trigger. 例如,如果要更改wtf元素的颜色,可以使用触发器进行更改。

<DataTemplate.Triggers> <DataTrigger Binding="{Binding ...}" Value="False"> <Setter TargetName="wtf" Property="Foreground" Value="DarkGreen"/> </DataTrigger> </DataTemplate.Triggers>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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