【问题标题】:Button in Custom ViewCell Xamarin Tableview C#自定义 ViewCell Xamarin Tableview C# 中的按钮
【发布时间】:2020-10-24 08:29:11
【问题描述】:

期待您的再次帮助:)

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

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

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

查看后面的单元格代码

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);

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

            tblData.Root.del(ROW_INDEX);

谢谢!

我相信我已经解决了这个问题。当我从第四个周末回来时将发布解决方案。

【问题讨论】:

  • 你可以在这里分享你的解决方案并接受它,这将帮助更多的人:)
  • @LucasZhang-MSFT 同意了。我本来打算。但是我必须先解决一个新问题。目前,由于某种未知原因,我无法在列表视图中设置选择器的值,这让我困惑了几天。任何帮助,将不胜感激。 stackoverflow.com/questions/62760135/…
  • 我已经发布了解决方案,你可以检查它,如果它帮助你不要忘记接受它:)

标签: c# xamarin


【解决方案1】:

此问题的解决方案已在另一篇文章中得到解答。基本上,一旦实现了 MVVM,解决方案就更容易了,尽管使用 listView 控件让它在选择器上工作很痛苦。

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

Trying to set picker within listview MVVM Xamarin

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

【讨论】:

    猜你喜欢
    • 2018-06-16
    • 2018-08-04
    • 2021-04-05
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2016-08-24
    相关资源
    最近更新 更多