Xaml-无法在类文件中获取项目名称(在listView中) [英] Xaml - Unable to get item Name in class file (inside listView)

查看:78
本文介绍了Xaml-无法在类文件中获取项目名称(在listView中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作XAML应用程序,但在将我的xaml文件与其类绑定时遇到一些问题.

I'm making an XAML app and i'm having some issues binding my xaml file with its class .

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="App.TestXaml"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

    <ContentPage.Content>

 <ListView x:Name="ListTest">

      <Label FontSize="12" x:Name="test1"/>


        <ListView.ItemTemplate>

                 <DataTemplate> 

                    <ViewCell>

                            <StackLayout Spacing="5" Orientation="Horizontal">

                                <Label FontSize="14" x:Name="test2"/>

                            </StackLayout>

                    </ViewCell>

                </DataTemplate>

          </ListView.ItemTemplate>

    </ListView>

    </ContentPage.Content>

</ContentPage>

在我的CS文件testXaml.cs中,该链接到该xaml的文件:

in my cs file testXaml.cs thats linked to this xaml :

test1.FontSize = 20 ; 

//有效-它检测到该标签的test1变量.

// Works - it detects the test1 variable for that label.

但是在我的列表视图中,当我尝试访问名为test2的标签时,它在我的cs文件中未检测到

In my listview however , when i try to access the label named test2 , it does not detect it in my cs file

test2.FontSize = 24 ; 

//该类无法检测到test2(名称test2在当前上下文中不存在)

// The class does not detect test2 (The name test2 does not exist in the current context)

有什么办法解决此问题或为CS文件中的列表项设置值吗?

Any idea how to fix this or set up a value for the list items from my cs file ?

推荐答案

将为ListView中的每个项目实例化DataTemplate.这意味着ListView中的 each 项目有一个test2的实例.假设您在ListView中有1,000个项目.您想到的是哪个test2?该框架无法猜测,也不会尝试.

The DataTemplate will be instantiated for each item in the ListView. That means there is an instance of test2 for each item in the ListView. Say you've got 1,000 items in the ListView. Which test2 did you have in mind? The framework can't guess and won't try.

您想要的是DataTemplate.Triggers.确切的细节将取决于您用来确定字体大小(不共享)的确切逻辑.这是DataTemplate示例,该示例使用触发器,您可以按原样粘贴并查看其工作原理.如果您给自己的DataTemplate一个或多个触发器,则看起来很像这样.在您的listview项模板中,DataTrigger.Binding将绑定到listview项的属性,无论它们是什么.

What you want here is DataTemplate.Triggers. The exact details are going to depend on the exact logic you're using to decide the font size, which you didn't share. This is an example of a DataTemplate which uses triggers, and which you can paste in as-is and see it work. If you give your own DataTemplate one or more triggers, that will look much like this. In your listview item template, DataTrigger.Binding will bind to properties of the listview items, whatever they may be.

如果您通过在代码后面插入ListViewItem来填充列表视图,那么您将借来很多您不需要的麻烦.

If you're populating your listview by instiating ListViewItem in codebehind, you're borrowing a lot of trouble you don't really need.

<ContentControl Content="Test">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Label 
                x:Name="myLabel"
                Content="{Binding}" 
                />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Length}" Value="4">
                    <Setter TargetName="myLabel" Property="Background" Value="LawnGreen" />
                    <Setter TargetName="myLabel" Property="FontSize" Value="40" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Length}" Value="5">
                    <Setter TargetName="myLabel" Property="Background" Value="OrangeRed" />
                    <Setter TargetName="myLabel" Property="FontSize" Value="9" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

这篇关于Xaml-无法在类文件中获取项目名称(在listView中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆