DotVVM Combobox control dynamic event handler

0

I have created a control in dotvvm to show a textbox with a value if it’s in view mode, otherwise show a combobox if mode is in add or edit.

<bs:TextBox Enabled="false" Text="{value: _control.TextValue}" Visible="{value: _control.IsViewMode}" />
<bs:ComboBox DataSource="{value: _control.DataList}" EmptyItemText="{value: _control.EmptyTextValue}" SelectedValue="{value: _control.SelectedValue}" Enabled="{value: !_control.IsViewMode }" Visible="{value: !_control.IsViewMode}" Style-border-color="{value: _control.IsProperyValid ? 'silver' : 'salmon'}"
             ItemValueBinding="{value: ID}" ItemTextBinding="{value: Name}" >
</bs:ComboBox>

Now, every viewmodel using this control has their own custom event to call if SelectionChanged invoke if the user selects an item in the combobox.

How can I achieve this? Thanks!

You can define a property of type Command on your control. If you use markup-defined properties, it will something like

@property string TextValue
...
@property Command Changed

<bs:ComboBox ... SelectionChanged={staticCommand: _control.Changed()}></bs:ComboBox>

If you want to defined the property in the code behind, it should be similar to any other DotvvmProperty, just the type is Command. If you only want to use command bindings, you can use command instead of staticCommand - I did use staticCommand because calling command from a staticCommand works fine, but the other around forces the staticCommand to also run in the full postback (eliminating the advatages of staticCommands).

Hi,

but how can I pass the command from the the dothml that extend the control?

like for my example, to pass a value to _control.DataList,

<cc: CustomControl DataList="{value: SampleList}">

Sorry for the delay, I missed the next question.

The Changed property is a normal property which should be bound to a command or staticCommand binding.

<cc: CustomControl DataList={value: SampleList} Changed={command: DoSomething()}>