[HtmlTargetElement("div", Attributes = StylePrefix + "*")]
public class SetStyleTagHelper : TagHelper
{
private const string StylePrefix = "style-";
[HtmlAttributeName("style")]
public string CssStyle { get; set; }
private IDictionary<string, string> _styleValues;
[HtmlAttributeName("", DictionaryAttributePrefix = StylePrefix)]
public IDictionary<string, string> StyleValues
{
get
{
return _styleValues ?? (_styleValues =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));
}
set { _styleValues = value; }
}
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var MystyleBuilder = new StringBuilder();
foreach (var style in _styleValues)
{
MystyleBuilder.Append($"{style.Key}:{style.Value}; ");
}
if (!string.IsNullOrEmpty(CssStyle))
{
MystyleBuilder.Insert(0, CssStyle + "; ");
}
if (MystyleBuilder.Length > 0)
{
output.Attributes.Add("style", MystyleBuilder.ToString());
}
}
}
Yorumlar
Yorum Gönder