Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1016997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:38:05+00:00 2026-05-16T10:38:05+00:00

I have read the GtkSharp TreeView tutorial wherein the author describes how to setup

  • 0

I have read the “GtkSharp TreeView tutorial” wherein the author describes how to setup and use a TreeModelFilter for an underlying ListStore ( under the tutorial section “Filtering Data”). The technique doesn’t seem to work for an underlying hierarchical TreeStore. I want to filter a multilevel TreeStore and show the results in a TreeView. Its giving me a real hard time. Are there any tutorials, samples, or suggestions for doing it ?

Following is the code. Its basically the same code as the tutorial except for changes to deal with construction and population of a TreeStore rather than a ListStore.
{The TreeStore is used to save “names” and “email addresses” of contacts , divided into (and saved as) children of the roots “friends” and “relatives” }

// compilation requires references to:
// gtk-sharp, atk-sharp and glib-sharp


using System;
using Gtk;

public class TreeViewExample
{
    public static void Main()
    {
        Gtk.Application.Init();
        new TreeViewExample();
        Gtk.Application.Run();
    }

    Gtk.Entry filterEntry;
    Gtk.TreeModelFilter filter;

    public TreeViewExample()
    {
        // Create a Window
        Gtk.Window window = new Gtk.Window("TreeView Example");
        window.SetSizeRequest(500, 200);
        window.DeleteEvent += delegate { Application.Quit(); };

        // Create an Entry used to filter the tree
        filterEntry = new Gtk.Entry();

        // Fire off an event when the text in the Entry changes
        filterEntry.Changed += OnFilterEntryTextChanged;

        // Create a nice label describing the Entry
        Gtk.Label filterLabel = new Gtk.Label("Search:");

        // Put them both into a little box so they show up side by side
        Gtk.HBox filterBox = new Gtk.HBox();
        filterBox.PackStart(filterLabel, false, false, 5);
        filterBox.PackStart(filterEntry, true, true, 5);

        // Create our TreeView
        Gtk.TreeView tv = new Gtk.TreeView();

        // Create a box to hold the Entry and Tree
        Gtk.VBox box = new Gtk.VBox();

        // Add the widgets to the box
        box.PackStart(filterBox, false, false, 5);
        box.PackStart(tv, true, true, 5);
        window.Add(box);

        //setting up columns and renderers
        Gtk.TreeViewColumn nameColumn = new Gtk.TreeViewColumn { Title = "Name" };
        Gtk.CellRendererText nameCell = new Gtk.CellRendererText();
        nameColumn.PackStart(nameCell, true);
        Gtk.TreeViewColumn emailColumn = new Gtk.TreeViewColumn { Title = "Email" };
        Gtk.CellRendererText emailCell = new Gtk.CellRendererText();
        emailColumn.PackStart(emailCell, true);

        // Add the columns to the TreeView
        tv.AppendColumn(nameColumn);
        tv.AppendColumn(emailColumn);

        // Tell the Cell Renderers which items in the model to display
        nameColumn.AddAttribute(nameCell, "text", 0);
        emailColumn.AddAttribute(emailCell, "text", 1);

        // Create a model that will hold two strings 
        Gtk.TreeStore contacts = new Gtk.TreeStore(typeof(string), typeof(string));

        // Add some hierarchical data
        Gtk.TreeIter treeiter;

        //first root
        treeiter = contacts.AppendValues("FRIENDS");

        // 2 children of first root
        contacts.AppendValues(treeiter, "Ogre", "stinky@hotmale.com");
        contacts.AppendValues(treeiter, "Bee", "stingy@coolguy.com");

        // second root
        treeiter = contacts.AppendValues("RELATIVES");

        // 3 children of second root
        contacts.AppendValues(treeiter, "Mommy", "mother@family.com");
        contacts.AppendValues(treeiter, "Daddy", "father@family.com");
        contacts.AppendValues(treeiter, "tom", "cousin@family.com");

        filter = new Gtk.TreeModelFilter(contacts, null);
        // Specify the function that determines which rows to filter out and which ones to display
        filter.VisibleFunc = new Gtk.TreeModelFilterVisibleFunc(FilterTree);

        // Assign the filter as our treeview's model
        tv.Model = filter;

        // Show the window and everything on it
        window.ShowAll();
    }

    private void OnFilterEntryTextChanged(object o, System.EventArgs args)
    {
        // Since the filter text changed, tell the filter to re-determine which rows to display
        filter.Refilter();
    }

    private bool FilterTree(Gtk.TreeModel model, Gtk.TreeIter iter)
    {
        string contactname = model.GetValue(iter, 0).ToString();
        if (filterEntry.Text == "")
            return true;
        if (contactname.IndexOf(filterEntry.Text) > -1)
            return true;
        else
            return false;
    }
}

[I am using mono 2.6.4 /monodevelop 2.4 / gtk-sharp 2.12 on windows vista.]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:38:05+00:00Added an answer on May 16, 2026 at 10:38 am

    It seems that when filtering rows in a tree model, a row is only visible if ALL its parents are visible too. Since your filter function hides the parent nodes, it will not display the child nodes even if the text matches. I have modified your code to illustrate this problem:

    Now, one of the parent nodes begins with ‘test’. If you type ‘test’ you’ll see the filtering works correctly.

    using System;
    using Gtk;
    
    public class TreeViewExample
    {
    
    public static void Main ()
    
    {
    
        Gtk.Application.Init ();
    
        new TreeViewExample ();
    
        Gtk.Application.Run ();
    
    }
    
    
    Gtk.Entry filterEntry;
    Gtk.TreeModelFilter filter;
    
    
    
    public TreeViewExample ()
    {
    
        // Create a Window
    
        Gtk.Window window = new Gtk.Window ("TreeView Example");
    
        window.SetSizeRequest (500,200);
    
        window.DeleteEvent+=delegate {Application.Quit();};
    
    
    
        // Create an Entry used to filter the tree
    
        filterEntry = new Gtk.Entry ();
    
    
    
        // Fire off an event when the text in the Entry changes
    
        filterEntry.Changed += OnFilterEntryTextChanged;
    
    
    
        // Create a nice label describing the Entry
    
        Gtk.Label filterLabel = new Gtk.Label ("Search:");
    
    
    
        // Put them both into a little box so they show up side by side
    
        Gtk.HBox filterBox = new Gtk.HBox ();
    
        filterBox.PackStart (filterLabel, false, false, 5);
    
        filterBox.PackStart (filterEntry, true, true, 5);
    
    
    
        // Create our TreeView
    
        Gtk.TreeView tv = new Gtk.TreeView ();
    
    
    
        // Create a box to hold the Entry and Tree
    
        Gtk.VBox box = new Gtk.VBox ();
    
    
    
        // Add the widgets to the box
    
        box.PackStart (filterBox, false, false, 5);
    
        box.PackStart (tv, true, true, 5);
    
    
    
        window.Add (box);
    
    
    
    
    
        //setting up columns and renderers
    
    
    
    
    
        Gtk.TreeViewColumn nameColumn = new Gtk.TreeViewColumn{Title="Name"}; 
    
        Gtk.CellRendererText nameCell = new Gtk.CellRendererText ();        
    
        nameColumn.PackStart (nameCell, true);
    
    
    
    
    
    
    
    
    
        Gtk.TreeViewColumn emailColumn = new Gtk.TreeViewColumn {Title="Email"}; 
    
        Gtk.CellRendererText emailCell = new Gtk.CellRendererText ();
    
        emailColumn.PackStart (emailCell, true);
    
    
    
    
    
    
    
        // Add the columns to the TreeView
    
        tv.AppendColumn (nameColumn);
    
        tv.AppendColumn (emailColumn);
    
    
    
    
    
    
    
        // Tell the Cell Renderers which items in the model to display
    
        nameColumn.AddAttribute (nameCell, "text", 0);
    
        emailColumn.AddAttribute (emailCell, "text", 1);
    
    
    
    
    
    
    
        // Create a model that will hold two strings 
    
        Gtk.TreeStore contacts = new Gtk.TreeStore (typeof (string), typeof (string));
    
    
    
    
    
        // Add some hierarchical data
    
    
    
        Gtk.TreeIter treeiter;
    
    
    
    
    
        //first root
    
        treeiter= contacts.AppendValues("testFRIENDS"); 
    
    
    
            // 2 children of first root
    
            contacts.AppendValues(treeiter, "testOgre", "stinky@hotmale.com");
    
            contacts.AppendValues(treeiter, "testBee", "stingy@coolguy.com");
    
    
    
    
    
    
    
        // second root
    
        treeiter= contacts.AppendValues("RELATIVES"); 
    
    
    
            // 3 children of second root
    
            contacts.AppendValues (treeiter,"Mommy","mother@family.com");
    
            contacts.AppendValues (treeiter,"Daddy", "father@family.com");
    
            contacts.AppendValues (treeiter,"tom", "cousin@family.com");
    
    
    
    
    
    
    
    
    
        filter = new Gtk.TreeModelFilter (contacts, null);
    
    
    
        // Specify the function that determines which rows to filter out and which ones to display
    
        filter.VisibleFunc = new Gtk.TreeModelFilterVisibleFunc (FilterTree);
    
    
    
        // Assign the filter as our treeview's model
    
        tv.Model = filter;
    
    
    
        // Show the window and everything on it
    
        window.ShowAll ();
    
    }
    
    
    
    private void OnFilterEntryTextChanged (object o, System.EventArgs args)
    
    {
    
        // Since the filter text changed, tell the filter to re-determine which rows to display
    
        filter.Refilter ();
    
    }
    
    
    
    private bool FilterTree (Gtk.TreeModel model, Gtk.TreeIter iter)
    
    {
    
        string contactname = model.GetValue (iter, 0).ToString ();
    
    
    
        if (filterEntry.Text == "")
    
            return true;
    
    
    
        if (contactname.IndexOf (filterEntry.Text) > -1)
    
            return true;
    
        else
    
            return false;
    
    }
    

    }

    The easiest solution with your current structure would be having the filter function always return TRUE for the ‘container’ nodes (Friends and Relatives), based upon a value in a hidden column in the model. It will not look exactly look the way you want, but it will work.

    The GTK+ Treeview Tutorial, though not updated for some time,is still a VERY useful resource for all your TreeView needs. The code and examples are in C, but most of it still applies to GTK#.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.