自定义 ViewCell Xamarin Tableview C# 中的按钮 [英] Button in Custom ViewCell Xamarin Tableview C#

查看:28
本文介绍了自定义 ViewCell Xamarin Tableview C# 中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次期待您的帮助:)

我试图让最右侧的按钮能够删除它们在 tableview 控件中的行.现在他们现在关于他们在哪一行,但我无法将此信息连接到父级.表格视图填充有自定义视单元.

I'm trying to have the buttons on the far right be able to delete their row in the tableview control. Right now they now about which row they are on but I cannot connect this information to the parent. The table view is populated with a custom viewcell.

自定义视图单元格包含两个不同的选择器、两个输入字段和一个按钮.我还没有找到一种更简洁的方法来执行此操作,因为我拥有与数据表控件中的行数无关的选择器数据.

The custom view cell contains two different pickers, two entry fields and a button. I haven't found a cleaner way to execute this as I have the picker's data which isn't related to the # of rows in the data table control.

目前,当您单击右侧的按钮时,它会将选中的行发布到控制台,但我不知道如何将其连接到其父级以便实际删除数据表上的该行

Currently when you click a button on the right it posts to the console what row was selected but I don't know of a way to connect that to its parent in order to actually delete that row on the data table

查看背后的单元格代码

public partial class RecipeIngredientViewCell : ViewCell
{
    ObservableCollection<clIngredient> _listIngredients = new ObservableCollection<clIngredient>();
    public ObservableCollection<clIngredient> listIngredients { get { return _listIngredients; } }

    ObservableCollection<clUnit> _listUnit = new ObservableCollection<clUnit>();
    public ObservableCollection<clUnit> funclistUnit { get { return _listUnit; } }


    clRecipeIngredient _recipeIngredient;
    int _row;

    public RecipeIngredientViewCell(clRecipeIngredient passedrecipeIngredient, ObservableCollection<clIngredient> passedlistIngredients, ObservableCollection<clUnit> passedlistUnits, int row)
    {
        InitializeComponent();

        _listIngredients = passedlistIngredients;
        _listUnit = passedlistUnits;
        _recipeIngredient = passedrecipeIngredient;

        _row = row;

        this.BindingContext = _recipeIngredient;

        //INGREDIENT PICKER
        pickerIngredient.ItemsSource = _listIngredients;
        for(int x = 0; x < _listIngredients.Count; x++)
        {
            if (_listIngredients[x].IngredientName == _recipeIngredient.IngredientName)
            {
                pickerIngredient.SelectedIndex = x;
            }
        }
        //UNIT PICKER
        pickerUnit.ItemsSource = _listUnit;
        for (int x = 0; x < _listUnit.Count; x++)
        {
            if (_listUnit[x].UnitName == _recipeIngredient.UnitName)
            {
                pickerUnit.SelectedIndex = x;
            }
        }


    }

    private void btnDeleteRecipeIngredient_Clicked(object sender, EventArgs e)
    {
        //NOT IMPLEMENTED YET!
        
        Console.WriteLine(_recipeIngredient.IngredientName + " AT ROW " + _row.ToString());
    }

    private void txtQuantity_TextChanged(object sender, TextChangedEventArgs e)
    {
        _recipeIngredient.Quantity = txtQuantity.Text.ToDouble();
    }

    private void txtComment_TextChanged(object sender, TextChangedEventArgs e)
    {
        _recipeIngredient.Comments = txtComment.Text;
    }

    private void pickerIngredient_SelectedIndexChanged(object sender, EventArgs e)
    {
        _recipeIngredient.IngredientName = pickerIngredient.SelectedItem.ToString();
    }

    private void pickerUnit_SelectedIndexChanged(object sender, EventArgs e)
    {
        _recipeIngredient.UnitName = pickerIngredient.SelectedItem.ToString();
    }
}

查看单元格 XAML

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="RecipeDatabaseXamarin.Views.RecipeIngredientViewCell">

<Grid VerticalOptions="CenterAndExpand" Padding = "20, 0" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="75" />
    </Grid.ColumnDefinitions>
    <Picker Grid.Column = "0" x:Name="pickerIngredient" HorizontalOptions = "StartAndExpand" SelectedIndexChanged="pickerIngredient_SelectedIndexChanged"/>
    <Entry Grid.Column = "1" x:Name ="txtQuantity" HorizontalOptions = "StartAndExpand" Text = "{Binding Quantity}" TextChanged="txtQuantity_TextChanged" />
    <Picker Grid.Column = "2" x:Name ="pickerUnit" HorizontalOptions = "StartAndExpand" SelectedIndexChanged="pickerUnit_SelectedIndexChanged"/>
    <Entry Grid.Column = "3" x:Name="txtComment"  HorizontalOptions = "StartAndExpand" Text = "{Binding Comments}" TextChanged="txtComment_TextChanged" WidthRequest="150"/>
    <Button Grid.Column = "4"  x:Name="btnDeleteRecipeIngredient" HorizontalOptions = "StartAndExpand" Text = "Delete Ingredient" Clicked="btnDeleteRecipeIngredient_Clicked"/>
</Grid>

页面背后的代码

        var section = new TableSection(); 
        for(int i = 0;i<_downloadedRecipeIngredients.Count;i++)
        {
            var cell = new RecipeIngredientViewCell(downloadedRecipeIngredients[i], listIngredients, listUnit, i);

            section.Add(cell);
        }
        tblData.Root.Add(section);

在后面的主页代码中,我希望按钮运行一段代码来执行诸如

In the main page code behind I want the button to run a block of code to execute something such as

            tblData.Root.del(ROW_INDEX);

谢谢!

我相信我已经解决了这个问题.我第 4 个周末回来时会发布解决方案.

Thanks!

I believe I have this solved. Will post the solution when I get back from the 4th weekend.

推荐答案

此问题的解决方案已在另一个帖子中回答.基本上,一旦实现了 MVVM,解决方案就更容易了,即使让它在带有 listView 控件的选择器上工作很痛苦.

The solution to this problem is answered on another post. Basically the solution is easier once MVVM is implemented even though it was a pain to get it to work on the picker withing the listView control.

这个其他线程有您可以运行的示例代码.

This other thread has sample code which you can run.

尝试在列表视图 MVVM Xamarin 中设置选择器

如果有人遇到同样的问题,请发帖,我会尽量回复,因为这个问题很痛苦!!!

If anyone runs into the same issue please post and I'll try to respond back as this issue was a PAIN!!!!

这篇关于自定义 ViewCell Xamarin Tableview C# 中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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