首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Xamarin在ContextAction中与C#结合形成ViewCell

Xamarin在ContextAction中与C#结合形成ViewCell
EN

Stack Overflow用户
提问于 2016-08-14 15:00:49
回答 2查看 3.1K关注 0票数 4

在我的视图模型中,下面的XAML正在绑定到delete命令:

代码语言:javascript
运行
复制
        <TextCell.ContextActions>
          <MenuItem Command="{Binding Path=BindingContext.DeleteCollectionCommand, Source={x:Reference Name=CollectionListView}}"
                    CommandParameter="{Binding .}"
                    Text="Delete" 
                    IsDestructive="True" />
        </TextCell.ContextActions>

我正在尝试将它转换为C#,这样我就可以编程地使用它了。

我试过以下几种方法,但不起作用。有什么需要改变的?是否有更好/不同的方式访问ViewModel DeleteCommand?

代码语言:javascript
运行
复制
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true }; // red background
        deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
        deleteAction.SetBinding(MenuItem.CommandProperty,
            new Binding("BindingContext.DeleteCommand", BindingMode.Default, null, null, null, "{x:Reference Name=CollectionBeerListView}"));

        ContextActions.Add(deleteAction);
    }

编辑

通过结合skar的答案和从父视图初始化单元格,我就能够做到这一点:

代码语言:javascript
运行
复制
        lstView.ItemTemplate = new DataTemplate(() =>
        {
            var cell = new DeleteGenericListItemTemplate(page);
            return cell;
        });

不确定这是否理想..。但却让我继续前进。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-16 06:22:31

如果您在文本单元格上扩展以创建如下内容,并引用您的页面,您应该能够通过页面的绑定上下文访问您的DeleteCommand:

代码语言:javascript
运行
复制
public class CustomCell: TextCell
{
        public CustomCell(Page page)
        {
            var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true }; // red background
            deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
            deleteButton.SetBinding (MenuItem.CommandProperty, new Binding ("BindingContext.DeleteCommand", source: page));
            ContextActions.Add(deleteAction);


        }
}
票数 1
EN

Stack Overflow用户

发布于 2021-02-02 15:41:26

我通过向我的(自定义)视图单元格添加一个ParentContext属性来修正这个问题。通过使用ViewModel将该属性设置为AncestorType,如下所示:

代码语言:javascript
运行
复制
ParentContext="{Binding ., Source={RelativeSource AncestorType={x:Type local:MyViewModel}}}" 

我的自定义视图单元格:

代码语言:javascript
运行
复制
public static readonly BindableProperty ParentContextProperty = BindableProperty.Create("ParentContext", typeof(object), typeof(CustomViewCell), null, BindingMode.OneTime);
    public object ParentContext
    {
        get { return GetValue(ParentContextProperty); }
        set { SetValue(ParentContextProperty, value); }
    }

然后,在我的viewcell中,我引用viewcell x:Name添加了contextaction,它可以工作:)

代码语言:javascript
运行
复制
<ViewCell.ContextActions>
<MenuItem 
    Command="{Binding ParentContext.TheCommand, Source={x:Reference customViewCellName}}" 
    CommandParameter="{Binding Id}"
    Text="Some text">
</MenuItem>
</ViewCell.ContextActions>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38943452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档