Skip to content

Instantly share code, notes, and snippets.

@dennis-garavsky
Last active October 27, 2015 16:21
Show Gist options
  • Select an option

  • Save dennis-garavsky/cc2f20b32027c123c6dc to your computer and use it in GitHub Desktop.

Select an option

Save dennis-garavsky/cc2f20b32027c123c6dc to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using DevExpress.Utils;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.Model;
using DevExpress.Utils.Serializing;
using DevExpress.ExpressApp.TreeListEditors.Win;
using DevExpress.XtraTreeList.StyleFormatConditions;
namespace YourSolution.Module.Win.Controllers {
// For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
public class T291012_TreeListEditor : ViewController<ListView>, IModelExtender {
TreeListFormatRulesStore formatRulesStore = null;
TreeListEditor treeListEditor = null;
protected override void OnActivated() {
base.OnActivated();
treeListEditor = View.Editor as TreeListEditor;
if(treeListEditor != null) {
View.ControlsCreated += View_ControlsCreated;
View.ModelSaved += View_ModelSaved;
}
}
private void View_ControlsCreated(object sender, EventArgs e) {
treeListEditor.TreeList.Load += TreeList_Load;
}
private void InitializeFormatRules() {
treeListEditor.TreeList.OptionsMenu.ShowConditionalFormattingItem = true;
formatRulesStore = new TreeListFormatRulesStore() { FormatRules = treeListEditor.TreeList.FormatRules };
formatRulesStore.Restore(((IModelListViewTreeFormatRuleSettings)View.Model).TreeFormattingSettings);
}
void TreeList_Load(object sender, EventArgs e) {
InitializeFormatRules();
}
void View_ModelSaved(object sender, EventArgs e) {
((IModelListViewTreeFormatRuleSettings)View.Model).TreeFormattingSettings = formatRulesStore.Save();
}
protected override void OnDeactivated() {
if(treeListEditor != null) {
if(treeListEditor.TreeList != null) {
treeListEditor.TreeList.Load -= TreeList_Load;
}
View.ModelSaved -= View_ModelSaved;
View.ControlsCreated -= View_ControlsCreated;
treeListEditor = null;
}
base.OnDeactivated();
}
public void ExtendModelInterfaces(ModelInterfaceExtenders extenders) {
extenders.Add<IModelListView, IModelListViewTreeFormatRuleSettings>();
}
}
public interface IModelListViewTreeFormatRuleSettings {
[Browsable(false)]
string TreeFormattingSettings { get; set; }
}
//Dennis: This is a modified version of the solution given at https://www.devexpress.com/Support/Center/Question/Details/T289562 that can save/load settings into/from a string.
internal class TreeListFormatRulesStore : IXtraSerializable {
TreeListFormatRuleCollection formatRules;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Browsable(true),
XtraSerializableProperty(XtraSerializationVisibility.Collection, true, false, true, 1000, XtraSerializationFlags.DefaultValue)]
public virtual TreeListFormatRuleCollection FormatRules {
get { return formatRules; }
set { formatRules = value; }
}
public string Save() {
using(MemoryStream stream = new MemoryStream()) {
SaveCore(new XmlXtraSerializer(), stream);
return Convert.ToBase64String(stream.GetBuffer());
}
}
public void Restore(string settings) {
if(!string.IsNullOrEmpty(settings)) {
using(MemoryStream stream = new MemoryStream(Convert.FromBase64String(settings))) {
RestoreCore(new XmlXtraSerializer(), stream);
}
}
}
void SaveCore(XtraSerializer serializer, object path) {
Stream stream = path as Stream;
if(stream != null)
serializer.SerializeObject(this, stream, GetType().Name);
else
serializer.SerializeObject(this, path.ToString(), GetType().Name);
}
void RestoreCore(XtraSerializer serializer, object path) {
Stream stream = path as Stream;
if(stream != null)
serializer.DeserializeObject(this, stream, GetType().Name);
else
serializer.DeserializeObject(this, path.ToString(), GetType().Name);
}
void XtraClearFormatRules(XtraItemEventArgs e) { FormatRules.Clear(); }
object XtraCreateFormatRulesItem(XtraItemEventArgs e) {
var rule = new TreeListFormatRule();
formatRules.Add(rule);
return rule;
}
#region IXtraSerializable
public void OnEndDeserializing(string restoredVersion) { }
public void OnEndSerializing() { }
public void OnStartDeserializing(LayoutAllowEventArgs e) { }
public void OnStartSerializing() { }
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment