使用Devexpress Cardview时如何根据table列值显示或隐藏元素

How to show or hide and element when using Devexpress Cardview based on table column value

我正在使用 Devexpress 的 CardView,我正在像这样绑定到数据库:

    var cardView = Html.DevExpress().CardView(settings =>
    {
      settings.Name = "MyCardView";
    }

    @cardView.BindToEF(string.Empty, string.Empty, (s, e) =>
    {
      e.QueryableSource = Helper.CustomerData();
    }).GetHtml()

现在我可以通过这样做将 link 添加到卡片顶部:

    var cardView = Html.DevExpress().CardView(settings =>
    {
      settings.Name = "MyCardView";
      settings.CardLayoutProperties.Items.AddGroupItem(g =>
      {
          g.ColSpan = 1;
          g.ShowCaption = DefaultBoolean.False;
          g.GroupBoxDecoration = GroupBoxDecoration.None;

          var cmdLayoutItem = new CardViewCommandLayoutItem();
          cmdLayoutItem.HorizontalAlign = FormLayoutHorizontalAlign.Right;

          var publicUrlButton = new CardViewCustomCommandButton();
          publicUrlButton.ID = "Link123";
          publicUrlButton.Text = "My Funny Url";

          cmdLayoutItem.CustomButtons.Add(publicUrlButton);
          g.Items.Add(cmdLayoutItem);
        });

        settings.Columns.Add(c =>
        {
            c.FieldName = "Id";
            c.Caption = "Id";
            c.ReadOnly = true;
            c.Visible = false;
        });

        settings.Columns.Add(c =>
        {
            c.FieldName = "IsOpen";
            c.Caption = "IsOpen";
            c.ReadOnly = true;
            c.Visible = false;
        });
    }

    @cardView.BindToEF(string.Empty, string.Empty, (s, e) =>
    {
      e.QueryableSource = Helper.CustomerData();
    }).GetHtml()

Helper.CustomerData() returns 一个 IQueryable<CustomerData>。现在假设我的 table CustomerData 有一个名为 IsOpen 的列。如何仅在 IsOpen 的值为 true 时显示 publicUrlButton

经过数小时的挖掘,终于找到了解决方案。 基本上您可以使用以下事件来控制行为:

        settings.CustomButtonInitialize = (s, e) =>
        {
            var cv = s as MVCxCardView;
            if (cv == null)
            {
                return;
            }

            var isOpen = cv.GetCardValues(e.VisibleIndex, "IsOpen");
            if (!isOpen)
            {
                if (e.ButtonID == "Link123")
                {
                    e.Visible = DefaultBoolean.False;
                }
            }
        };