简体   繁体   English

XAML如何访问ListView内的Button?

[英]XAML How do I access Button that's inside a ListView?

I have the following ListView which displays a button for each item in the list. 我有以下ListView,它为列表中的每个项目显示一个按钮。 I want the buttons to pass through their primary key 'BoxID' when clicked and open up a new page. 我希望按钮在单击时能够通过其主键“ BoxID”传递并打开一个新页面。 The problem arises due to the corresponding cs document not being able to reference the button. 由于相应的cs文档无法引用该按钮而导致出现问题。 Why is the cs function not able to access this button? 为什么cs函数无法访问此按钮? Is there a way around this? 有没有解决的办法?

XAML: XAML:

<ListView x:Name="boxList" ItemsSource="{Binding Boxes}">
    <ListView.ItemTemplate> 
        <DataTemplate> 
            <ViewCell>
                <ViewCell.View>
                    <StackLayout Orientation="Horizontal" Padding="10,0">
                        <StackLayout HorizontalOptions="StartAndExpand">
                            <Button x:Name="editBoxButton" Text="{Binding 
                                    BoxName}" CommandParameter="{Binding 
                                    BoxID}"
                                    FontAttributes="Bold" Clicked="editBox" 
                                    HeightRequest="75" WidthRequest="150" 
                                    FontSize="Medium" BorderColor="Black"/>
                        </StackLayout>
                        <Label HorizontalOptions="EndAndExpand" 
                        VerticalOptions="Center" Text="{Binding Complete}"/>
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

CS: CS:

private void editBox(object sender, EventArgs e) {
    int temp_boxID = editBoxButton.CommandParameter;
    Navigation.PushModalAsync(new EditBoxPage(temp_boxID));
}

Thanks 谢谢

The reason why it's not able to reference to that button is because it's in a DataTemplate . 之所以不能引用该按钮,是因为它在DataTemplate

Now how can you solve it? 现在如何解决呢? It's simple, since the event listener is the clicked event of the button, just cast the sender as a button. 这很简单,因为事件侦听器是按钮的clicked事件,所以只需将发送方转换为按钮即可。 Now you can further simplyfy your work by accessing DataContext if it's UWP or the BindingContext if it's xamarin (since you've mentioned android tag too). 现在,您可以通过访问DataContext如果它是UWPBindingContext如果它是xamarin来进一步简化工作(因为您也提到了android标签)。 Your modified for XAMARIN code is below: 您为XAMARIN代码修改的XAMARIN如下:

 private void editBox(object sender, EventArgs e)
    {

        //if you're using VS2017 with c# 7.0
        if (sender is Button editBoxButton)
        {
            if (editBoxButton.CommandParameter is int temp_boxID)
                Navigation.PushModalAsync(new EditBoxPage(temp_boxID));
        }

        //if not then we'll go the traditional way
        var editBoxButton = sender as Button;
        if(editBoxButton!=null)
        {
            var boxID = editBoxButton.CommandParameter;
            int temp_boxID;
            int.TryParse(boxID.ToString(), out temp_boxID);
            Navigation.PushModalAsync(new EditBoxPage(temp_boxID));

        }
    }

I've put two ways of doing it based on the version of Visual Studio you're using and the version of Language you're using C# 6.0 or C# 7.0 我根据您使用的Visual Studio版本和您使用的C# 6.0C# 7.0语言版本,提出了两种处理方法

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

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