我创建了自己的Code for Accordion组件。它看起来如下图所示。
带有蓝色箭头的行是文章部分,带有红线的行是文章。我想要的是页面加载我将只获得根文章部分,没有别的。当我点击" TEST"我将发送带ID的get请求以加载更多项目。
我的问题是我无法获取在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>