Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
325
Select Parent and change style property
posted

Hello,

I have a datagrid view setup to mimic a treeview.

When a user selects a deeply nested 'node' and clicks a button on the form I need it to select only it's parent nodes and their parent's nodes then make them bold.

I have:

        private void MarkAsAdded(object p)
        {
            try
            {
               
                DataRecord addedRecord = (DataRecord)p;
                DataRecordPresenter drp = (DataRecordPresenter)DataRecordPresenter.FromRecord(addedRecord) as DataRecordPresenter;

                addedRecord.Cells["IsAdded"].Value = "True";
                drp.FontWeight = FontWeights.Bold;

                if (addedRecord.Cells["IsFolder"].Value.ToString() == "True")
                {
                    if (addedRecord.ChildRecords.Count > 0)
                    {
                        addedRecord.Cells["HasAddedChildren"].Value = "True";
                        drp.FontStyle = FontStyles.Italic;
                    }
                }

                if (addedRecord.ParentRecord != null)
                {
                    bool done = false;

                    DataRecord dr = (DataRecord)addedRecord.ParentDataRecord;

                    do
                    {
                        if (dr != null)
                        {
                            DataRecordPresenter tdrp = DataRecordPresenter.FromRecord(dr) as DataRecordPresenter;

                            dr.Cells["HasAddedChildren"].Value = "True";
                            tdrp.FontStyle = FontStyles.Italic;

                            dr = (DataRecord)dr.ParentDataRecord;
                        }
                        else
                        {
                            done = true;
                        }
                    } while (done == false);
                }
            }
            catch (Exception ex)
            {
               
            }

So far, but it is buggy.

and selects the parent... and it's children. then bolds them.

Is there a solution to this?

 

Thanks,

Matt.

  • 69686
    Verified Answer
    posted

    Hello Matt,

    This looks quite complex to me of achieving this goal. Here is one recursive method of doing this:

    1. When you select a record, set its FontWeight to Bold. You can do that by handling the RecordActivated Event of the XamDataGrid and call the following method:

    HightLightExpanded(e.Record as DataRecord); where:

     

    private void HightLightExpanded(DataRecord dataRecord)

            {

                DataRecord dr = dataRecord;

                if (dr == null)

                    return;

                dr.Tag = "Bold";

                HightlightParents(dr);

            }

    2. The HightlightParents method recursively gets the parents and highlights them as well

     

    private void HightlightParents(DataRecord dataRecord)

            {

                DataRecord parent = dataRecord.ParentDataRecord;

                if (parent == null)

                {

                    return;

                }

                else

                {

                    parent.Tag = "Bold";

                    if(parent.ParentDataRecord!=null)

                    HightlightParents(parent.ParentDataRecord);

                }

            }

    3. Here is the style for the DataRecordPresenter which actually high lights the records:

     

    <Style TargetType="{x:Type igDP:DataRecordPresenter}">

            <Setter Property="FontWeight" Value="{Binding Path=Tag}"/>

        </Style>

    As you can see, I am using a style that binds the Tag to the FontWeight. The reason I am doing this is because I want to avoid getting the DataRecordPresenter (makes the code more unreadable) in c#.

    4. Reset the FontWeight when you change the selection. You can do this either before a new selection or in the RecordDeactivating event. You can again use similar recursive function to reset the Tag the same way you set it.

    Hope this helps.