provide control developer specified custom style classes and properties

This commit is contained in:
Michael Becker 2024-08-09 22:22:54 -04:00
parent 9a4dbda5df
commit c43eda9152
2 changed files with 54 additions and 1 deletions

View File

@ -1,3 +1,4 @@
using System.Text;
using System.Xml;
using MBS.Web.UI;
@ -13,6 +14,7 @@ public abstract class Control : IWebHandler
}
public Dictionary<string, string> PathVariables { get; } = new Dictionary<string, string>();
public Dictionary<string, string> StyleProperties { get; } = new Dictionary<string, string>();
public bool Visible { get; set; } = true;
public Control(Dictionary<string, string>? pathVariables = null)
@ -39,6 +41,12 @@ public abstract class Control : IWebHandler
return new string[0];
}
private IReadOnlyDictionary<string, string> _emptyStyleProperties = new Dictionary<string, string>();
protected virtual IReadOnlyDictionary<string, string> GetStyleProperties()
{
return _emptyStyleProperties;
}
private bool _initted = false;
protected void EnsureInitialized()
{
@ -65,6 +73,8 @@ public abstract class Control : IWebHandler
writer.WriteStartElement(TagName);
bool classListWritten = false;
bool styleListWritten = false;
IDictionary<string, string> attrs = GetControlAttributes();
foreach (KeyValuePair<string, string> kvp in attrs)
{
@ -76,6 +86,13 @@ public abstract class Control : IWebHandler
value = String.Join(' ', classList);
classListWritten = true;
}
else if (kvp.Key == "style")
{
// IDictionary<string, string> classList = GetStyleProperties().Union(kvp.Value.Split(new char[] { ';' }));
// value = String.Join(' ', classList);
// classListWritten = true;
}
writer.WriteAttributeString(kvp.Key, value);
}
@ -84,8 +101,28 @@ public abstract class Control : IWebHandler
{
writer.WriteAttributeString("class", String.Join(' ', actualClassList));
}
IEnumerable<KeyValuePair<string, string>> actualStyleProperties = GetStyleProperties().Union(StyleProperties);
if (!styleListWritten && actualStyleProperties.Count() > 0)
{
writer.WriteAttributeString("style", RenderStyleProperties(actualStyleProperties));
}
}
protected virtual void RenderEndTag(XmlWriter writer)
private string RenderStyleProperties(IEnumerable<KeyValuePair<string, string>> actualStyleProperties)
{
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> kvp in actualStyleProperties)
{
sb.Append(kvp.Key);
sb.Append(':');
sb.Append(kvp.Value);
sb.Append(';');
}
return sb.ToString();
}
protected virtual void RenderEndTag(XmlWriter writer)
{
writer.WriteEndElement();
}

View File

@ -113,6 +113,18 @@ public class ListView : Container
private HtmlGenericControl thead = null, tbody = null;
protected override IEnumerable<string> GetStyleClasses()
{
return [ "uwt-listview" ];
}
protected override IReadOnlyDictionary<string, string> GetStyleProperties()
{
return new Dictionary<string, string>(new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("height", "300px")
});
}
protected override IList<Control> GetChildControls()
{
thead = new HtmlGenericControl("thead");
@ -127,6 +139,10 @@ public class ListView : Container
thead.Controls.Add(tr);
tbody = new HtmlGenericControl("tbody");
tbody.StyleProperties["height"] = "500px";
tbody.StyleProperties["overflow"] = "scroll";
tbody.StyleProperties["display"] = "block";
foreach (ListViewItem lvi in Items)
{
HtmlGenericControl tr2 = new HtmlGenericControl("tr");