How to handle the width of customized items?

1 posts, 0 answers
  1. 红耀
    红耀 avatar
    9 posts
    Member since:
    Aug 2018

    Posted 24 Aug 2018 Link to this post

    I want to develop a WinForms application which is similar with Outlook. For the email list part, I use the custom item in the ListView to display the email information(From, Subject, Receive Time, Body Content). The ListView control is inside a split container(Dock Fill). So that, user can change the width of ListView. 

    The problem is, when the subject or body content is very long, I don't want the item to auto size. I want the item hidden the text when overflow. When user changes the width of ListView via split container, the text can be shown more or less according to the width of ListView. It likes in the outlook, you can change the width of the email list, the body content and the subject can be automatically displayed to fit the width. How to implement this function by using ListView?

    My custom item as below:

    private class MailListVisualItem : SimpleListViewVisualItem
            {
                private LightVisualElement element1;
                private LightVisualElement element2;
                private StackLayoutPanel layout;
     
                protected override void CreateChildElements()
                {
                    base.CreateChildElements();
     
                    this.layout = new StackLayoutPanel();
                    this.layout.EqualChildrenWidth = false;
                    this.layout.Margin = new Padding(20, 0, 0, 0);
     
                    this.element1 = new LightVisualElement();
                    element1.TextAlignment = ContentAlignment.TopLeft;
                    element1.Size = new Size(260, 0);
                    element1.MinSize = new Size(260, 0);
                    element1.NotifyParentOnMouseInput = true;
                    element1.ShouldHandleMouseInput = false;
                    this.layout.Children.Add(this.element1);
     
                    this.element2 = new LightVisualElement();
                    element2.TextAlignment = ContentAlignment.TopLeft;
                    element2.Size = new Size(80, 0);
                    element2.NotifyParentOnMouseInput = true;
                    element2.ShouldHandleMouseInput = false;
                    this.layout.Children.Add(this.element2);
     
                    this.Children.Add(this.layout);
                }
     
                protected override void SynchronizeProperties()
                {
                    base.SynchronizeProperties();
     
                    if (this.Data.GetType() != typeof(ListViewDataItemGroup))
                    {
                        this.Text = "";
                        var bodyText = this.Data["BodyText"] == null ? "" : this.Data["BodyText"].ToString().Replace(System.Environment.NewLine, " ");
     
                        this.element1.Text = "<html><span>" +
                            "<span style=\"font-size:12\">" + this.Data["From"] + "</span>" +
                            "<br><span style=\"color:#13224D;font-family:Segoe UI;font-size:10\">" + this.Data["Subject"] + "</span>" +
                            "<br><span style=\"color:#13224D;font-family:Segoe UI;font-size:10\">" + bodyText + "</span></span></html>";
     
                        this.element2.Text = "<html><span> </span>" +
                            "<br><span style =\"color:#13224D;font-family:Segoe UI;font-size:10\">" + this.Data["ReceivedTime"] + "</span></html>";
     
                        this.TextAlignment = ContentAlignment.TopLeft;
                    }
                }
     
                protected override Type ThemeEffectiveType
                {
                    get
                    {
                        return typeof(SimpleListViewVisualItem);
                    }
                }
            }

Back to Top