1

我为 Accordion 创建了自己的纯代码组件。如下图所示。

在此处输入图像描述

带有蓝色箭头的行是文章部分,带有红色箭头的行是文章。我想要归档的是页面加载,我只会得到根文章部分,没有别的。当我单击“测试”时,我将发送带有 ID 的获取请求以加载更多项目。

我的问题是我无法获取在 ViewModel 中单击属性的对象。我SelectedValueProperty为此目的创建并在static constructor. 之后我重写GetControlBinding方法并将属性添加到组中。但是,当我单击视图中的项目时,没有任何内容分配给我的SelectedValueProperty.

这是我的 CoC 代码

public class Accordion : HierarchyItemsControlBase
{
    public static readonly DotvvmProperty SelectedValuesProperty;
    public static readonly DotvvmProperty ChangedProperty;
    protected override HtmlContents CreateItemContents(DataItemContainer dataItem)
    {
        var contents = base.HtmlFactory.CreateContents();
        return contents;
    }

    public Accordion() : base("div")
    {

    }

    static Accordion()
    {
        SelectedValuesProperty = DotvvmProperty.Register<IEnumerable, Accordion>(c => c.SelectedValues, null, false);
        ChangedProperty = DotvvmProperty.Register<Command, Accordion>(c => c.Changed, null);
    }

    #region Properties

    public List<ArticleSectionListDTO> DataSource
    {
        get => (List<ArticleSectionListDTO>)GetValue(DataSourceProperty);
        set => SetValue(DataSourceProperty, value);
    }

    public IEnumerable SelectedValues
    {
        get => GetValue(SelectedValuesProperty, true) as IEnumerable;
        set => SetValue(SelectedValuesProperty, value);
    }

    public Command Changed
    {
        get => GetValue(ChangedProperty) as Command;
        set => SetValue(ChangedProperty, value);
    }

    public Command Click { get; set; }
    #endregion


    protected override ControlBindingGroup GetControlBinding()
    {
        var controlBinding = base.GetControlBinding();
        controlBinding.Add(this, SelectedValuesProperty);
        return controlBinding;
    }


    protected override void AddAttributesToRender(IHtmlWriter writer, IDotvvmRequestContext context)
    {
        writer.AddControlKnockoutDataBind<Accordion>(GetControlBinding());

        Attributes.Add("class", "accordion");
        base.AddAttributesToRender(writer, context);

        var clickBinding = GetCommandBinding(ChangedProperty);
        if (clickBinding != null)
        {
            writer.AddAttribute("onclick", KnockoutHelper.GenerateClientPostBackScript(nameof(Click), clickBinding, this), true, ";");
        }
    }

    public void DataBind(IDotvvmRequestContext context)
    {
        //some code here
    }

    public void DataBindItem(DotvvmControl parent, ArticleSectionListDTO item, IDotvvmRequestContext context)
    {
         //some code here
    }

    protected override void RenderContents(IHtmlWriter writer, IDotvvmRequestContext context)
    {
        DataBind(context);
        base.RenderContents(writer, context);
    }
}

这是我的 ViewModel,AccordionList它用作数据源,并且AccordionSelected必须用作我单击的对象的存储。

public List<ArticleSectionListDTO> AccordionList { get; set; } = new List<ArticleSectionListDTO>();
public List<ArticleSectionListDTO> AccordionSelected { get; set; } = new List<ArticleSectionListDTO>();

我如何在视图中使用它

<coc:Accordion DataSource="{value: AccordionList}" 
       ItemChildrenBinding="{{value: HasCategories}}" 
       SelectedValues="{{value: AccordionSelected}}">
</coc:Accordion>     
4

0 回答 0