ImageGetter cambio immagini DataTreeListView

di il
5 risposte

ImageGetter cambio immagini DataTreeListView

Ciao a tutti,

   nell'esempio sotto la procedura imagegetter modifica l'immagine della radice dell'albero con un semplice + se il nodo ha un figlio. Vorrei poter accedere all'oggetto row in modo da modificare l'immagine scelta all'interno della raccolta imagelist a seconda se il nodo ha oppure no il figlio ed eventualmente inserire una immagine diversa anche nel figlio. Esempio questo:

Alcune soluzioni indicano di utilizzare un cast ma non saprei come implementarlo. 

Grazie in anticipo per i vostri suggerimenti.

namespace ObjectListViewDemo
{
    public partial class TabDataTreeListView : OlvDemoTab {

        public TabDataTreeListView()
        {
            InitializeComponent();
            this.ListView = this.olvDataTree;
        }

        protected override void InitializeTab() {

            // The whole point of a DataTreeListView is to write no code. So there is very little code here.

            // Put some images against each row
            this.olvColumn41.ImageGetter = delegate(object row) { return "user"; };

            // The DataTreeListView needs to know the key that identifies root level objects.
            // DataTreeListView can handle that key being any data type, but the Designer only deals in strings.
            // Since we want a non-string value to identify keys, we have to set it explicitly here.
            this.olvDataTree.RootKeyValue = 0u;

            // Finally load the data into the UI
            LoadXmlIntoTreeDataListView();

            // This does a better job of auto sizing the columns
            this.olvDataTree.AutoResizeColumns();
        }

        private void LoadXmlIntoTreeDataListView() {
            DataSet ds = Coordinator.LoadDatasetFromXml(@"Data\FamilyTree.xml");

            if (ds.Tables.Count <= 0) {
                Coordinator.ShowMessage(@"Failed to load data set from Data\FamilyTree.xml");
                return;
            }

            this.dataGridView2.DataSource = ds;
            this.dataGridView2.DataMember = "Person";

            // Like DataListView, the DataTreeListView can handle binding to a variety of sources
            // And again, you could create a BindingSource in the designer, and assign that BindingSource
            // to DataSource, removing the need to even write these few lines of code.

            //this.olvDataTree.DataSource = new BindingSource(ds, "Person");
            //this.olvDataTree.DataSource = ds.Tables["Person"];
            //this.olvDataTree.DataSource = new DataView(ds.Tables["Person"]);
            //this.olvDataTree.DataMember = "Person"; this.olvDataTree.DataSource = ds;
            this.olvDataTree.DataMember = "Person";
            this.olvDataTree.DataSource = new DataViewManager(ds);
        }

        #region UI event handlers

        private void filterTextBox_TextChanged(object sender, EventArgs e)
        {
            Coordinator.TimedFilter(this.ListView, ((TextBox)sender).Text);
        }

        private void buttonResetData_Click(object sender, EventArgs e)
        {
            LoadXmlIntoTreeDataListView();
        }

        #endregion
    }
}

5 Risposte

  • Re: ImageGetter cambio immagini DataTreeListView

    Ciao,

    prova ad usare la GetChildren() per ottenere la lista dei figli se sono presenti oppure no.

    if (this.olvDataTree.GetChildren(row).Count > 0)

    :
    Fare attenzione se carichi i nodi della datatree con il metodo Lazy 

  • Re: ImageGetter cambio immagini DataTreeListView

    Grazie By65Franco, sicuramente la tua riga di codice mi servirà come controllo.

    ieri sera prima di andare a letto mi è venuta una idea :-) . Provata questa mattina e sembra funzionare. Da ottimizzare naturalmente.

    this.olvDataTree.AllColumns[0].ImageGetter = delegate (object row) 
    { 
        DataRowView ds = (DataRowView)row;
        DataRow r= ds.Row;
        object x = r.ItemArray[1];
        string h = x.ToString();
        if (h == "0") { 
            return 1;  
        }
        else
        {
            return 0;
        }
    };
  • Re: ImageGetter cambio immagini DataTreeListView

    15/05/2025 - KronS ha scritto:

    ieri sera prima di andare a letto mi è venuta una idea :

    Ciao,

    prova a semplificare e vai di lambata ;-)    ...dovrebbe funzionare, fai test.

    this.olvDataTree.AllColumns[0].ImageGetter = row =>
    {
        var r = ((DataRowView)row).Row;
        return r[1].ToString() == "0" ? 1 : 0;
    };

    Evita array e variabili, risulta più leggero e performante

  • Re: ImageGetter cambio immagini DataTreeListView

    15/05/2025 - By65Franco ha scritto:

    prova a semplificare e vai di lambata ;-)    ...dovrebbe funzionare, fai test.

    this.olvDataTree.AllColumns[0].ImageGetter = row =>
    {
        var r = ((DataRowView)row).Row;
        return r[1].ToString() == "0" ? 1 : 0;
    };

    Questa mi è piaciuta!

    Provata e funziona correttamente. Grazie.

  • Re: ImageGetter cambio immagini DataTreeListView

    Domanda se posso... solo per capire meglio.

    tu vuoi ottenere un effetto di questo tipo ?

    dove i nodi di tipo Folder la cartella si Apre e si Chiude secondo se hai espanso o meno il ramo della TreeView ?

Devi accedere o registrarti per scrivere nel forum
5 risposte