Hi guys
Iām trying to make a custom control. I chose to make the control as a Markup control (DotvvmMarkupControl) so that I donāt have to create the HTML structure of the control with C# code as it would be with a Code-only control, but the result should behave like a Code-only control.
I have a few issues.
The first is that I donāt want to have any viewmodel here (like in the Code-only control), the properties will be bind from the viewmodel when the control wil be used in application.
@viewmodel is required here, but I got around that using:
@viewModel System.Object
but itās not completely clean solution.
The second thing is about properties. I would need to make a derived property, I would like to do something like:
public string MessageTypeClass => this.MessageType switch
{
MessageType.Info => "alert-info",
MessageType.Warning => "alert-warning",
_ => "alert-danger",
};
But in this case property MessageTypeClass isnāt working.
So I instead of this do a complete DotvvmProperty and set its value in OnPreRender method. This partially works, but after postback is sometimes property not updated correctly.
My code is this:
dotcontrol:
@viewModel System.Object
@baseType DC.HodnoceniZamestnancu.Controls.MessageAlert, DC.HodnoceniZamestnancu
{{value: _control.MessageType}}
{{value: _control.MessageTypeClass}}
and code behind:
public class MessageAlert : DotvvmMarkupControl
{
public static readonly DotvvmProperty MessageTypeProperty = DotvvmProperty.Register<MessageType, MessageAlert>(c => c.MessageType, MessageType.Error);
public static readonly DotvvmProperty MessageTypeClassProperty = DotvvmProperty.Register<string, MessageAlert>(c => c.MessageTypeClass, "alert-danger");
public MessageType MessageType
{
get { return (MessageType)GetValue(MessageTypeProperty)!; }
set { SetValue(MessageTypeProperty, value); }
}
public string MessageTypeClass
{
get { return (string)GetValue(MessageTypeClassProperty)!; }
set { SetValue(MessageTypeClassProperty, value); }
}
protected override void OnPreRender(IDotvvmRequestContext context)
{
this.MessageTypeClass = this.MessageType switch
{
MessageType.Info => "alert-info",
MessageType.Warning => "alert-warning",
MessageType.Success => "alert-success",
_ => "alert-danger",
};
base.OnPreRender(context);
}
}
And control is used like this, where MessageType is binded from viewmodel:
<cc:MessageAlert MessageType=ā{value: PageErrorMessageType}ā />
What is the correct way to do this derived property logic in code behind? For example, in XAML in DependencyProperty there was something called coerce-value callbacks for this. Is here something similar?
Thanks.