Domanda

I cannot seem to find anywhere, any examples on how to make use of the GroupFormatter delegate to allow me to add footers to my groups when using the ObjectListView control.

Does anyone have any examples that could demonstrate this? I want to remove the text from the group header and add a footer (different text per footer). As well as changing font, etc.

Any examples would be very helpful.

È stato utile?

Soluzione

You can analyze the code for the

public void MakeGroupies<T>(T[] values, string[] descriptions, object[] images, string[] subtitles, string[] tasks)

method of the ObjectListView class. That explicitly sets the GroupKeyGetter, GroupKeyToTitleConverter and GroupFormatter property delegates.

This is C# but your VB adaptation should be straightforward. I am using this small test class as the object type to bind to the list view.

public class TestClass
{
    private readonly string _s;
    private readonly float _f;

    public TestClass( string p1, float p2 )
    {
        this._s = p1;
        this._f = p2;
    }

    [OLVColumn(DisplayIndex = 1, Name="S", Title="String")]
    public string S {get {return this._s;}}
    [OLVColumn( DisplayIndex = 2, Name = "F", Title = "Float" )]
    public float F {get {return this._f;}}
}

So as not to manually define column traits I am using attributes inside the bound object and a

BrightIdeasSoftware.Generator.GenerateColumns( this.olv, typeof( TestClass ) );

call in the form/user control where I am using the list view. In fact here is the method that completely isolates ObjectListView configuration:

        void SetData( TestClass[] objects )
        {
            // build list columns
            Generator.GenerateColumns( this.olv, typeof( TestClass ) );
            // use groups and make current column the priimary sort column
            this.olv.ShowGroups = true;
            this.olv.SortGroupItemsByPrimaryColumn = false;
            // loop through columns and set properties            
            foreach( OLVColumn col in this.olv.Columns )
            {
                col.Groupable = true;
                col.Sortable = true;
                if( col.Name == "F" )
                { 
                    col.MakeGroupies<float>( new float[] { 10f, 100f, 1000f }, new string[] { "<10", "10-100", "100-1000", ">1000" } );
                }
                else if( col.Name == "S" )
                {
                    col.UseInitialLetterForGroup = false;
                    //
                    col.GroupKeyGetter = ( obj ) =>
                    {
                        TestClass tc = (TestClass)obj;
                        switch( char.ToLower( tc.S[0] ) )
                        {
                            case 'a':
                            case 'e':
                            case 'i':
                            case 'o':
                            case 'u': return true;
                            default: return false;
                        }
                    };
                    //
                    col.GroupKeyToTitleConverter = ( o ) => { bool b = (bool)o; return b ? "vowel" : "consonant"; };
                    //
                    col.GroupFormatter = ( /*OLVGroup*/ group, /*GroupingParameters*/ parms ) => 
                    { 
                        string s = string.Format ("{0} {1}", group.GroupId, group.Id);
                        //group.BottomDescription = "BottomDescription: " + s;
                        //group.TopDescription = "TopDescription: " + s;
                        group.Footer = "Footer: " + s;                        
                    };

                }

            }
            //
            this.olv.RebuildColumns();
            //
            this.olv.SetObjects( objects );
        }

You will definitely have one different footer per each group.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top