Setting a ContentControl Checkbox state?

Jan 21, 2016 at 8:31 PM
Edited Jan 22, 2016 at 3:58 PM
First, thanks for this awesome contribution. I was able to quickly cobble together an application that allows me to populate a fairly complex template that represents a special education Individualized Education Program (IEP). The document is quite large and mostly contains rich text fields and nearly 70 checkboxes.

It's the checkboxes that I'm having a tough time getting figured out. Any hints as to how I get at (and update) the <w14:checked w14:val="0"/> element using the ContentControl's w:tag tag?
<w:sdtPr>
    <w:tag w:val="{{PLEP_English_CB}}"/>
    <w:id w:val="-1291594214"/>
    <w:lock w:val="sdtLocked"/>
    <w14:checkbox>
        <w14:checked w14:val="0"/>
        <w14:checkedState w14:val="2612" w14:font="MS Gothic"/>
        <w14:uncheckedState w14:val="2610" w14:font="MS Gothic"/>
    </w14:checkbox>
</w:sdtPr> 
Thanks -- Steve
Jan 22, 2016 at 4:25 PM
Ok. I was able to figure how to do it but it's pretty convoluted. For anyone looking to do something similar this should hopefully get you started.

Modify the OpenXmlHelper SetContentOfContentControl method as follows:
         public void SetContentOfContentControl(SdtElement contentControl, string content)
        {
            if (contentControl == null)
            {
                throw new ArgumentNullException("contentControl");
            }

            content = string.IsNullOrEmpty(content) ? string.Empty : content;
            bool isCombobox = contentControl.SdtProperties.Descendants<SdtContentDropDownList>().FirstOrDefault() != null;
            bool isCheckBox = contentControl.SdtProperties.Descendants<SdtContentCheckBox>().FirstOrDefault() != null;            

            if (isCombobox)
            {
                OpenXmlCompositeElement openXmlCompositeElement = GetSdtContentOfContentControl(contentControl);
                Run run = CreateRun(openXmlCompositeElement, content);
                SetSdtContentKeepingPermissionElements(openXmlCompositeElement, run);
            }

            else if (isCheckBox)
            {
                SdtContentCheckBox cb = contentControl.SdtProperties.Descendants<SdtContentCheckBox>().FirstOrDefault();

                if (cb != null)
                {
                    bool isChecked;

                    if (bool.TryParse(content, out isChecked))
                    {
                        // Set the checked value...
                        cb.Checked.Val = isChecked ? OnOffValues.One : OnOffValues.Zero;
                        
                        // We'll also need to update the Text property to include the checked or unchecked symbol...
                        string checkedChar = "☒";
                        string uncheckedChar = "☐";
                        string curCheckedStateChar = isChecked ? checkedChar : uncheckedChar;
                        OpenXmlElement grandParent = cb.Parent.Parent;

                        List<Text> textList = grandParent.Descendants<Text>().ToList();

                        foreach (Text t in textList)
                        {
                            string s = t.Text;

                            if (s.IndexOf(checkedChar) != -1)
                                t.Text = s.Replace(checkedChar, curCheckedStateChar);

                            else if (s.IndexOf(uncheckedChar) != -1)
                                t.Text = s.Replace(uncheckedChar, curCheckedStateChar);
                        }
                    }
                }
            }
            
            else
            {
                OpenXmlCompositeElement openXmlCompositeElement = GetSdtContentOfContentControl(contentControl);
                contentControl.SdtProperties.RemoveAllChildren<ShowingPlaceholder>();
                List<Run> runs = new List<Run>();

                if (IsContentControlMultiline(contentControl))
                {
                    List<string> textSplitted = content.Split(Environment.NewLine.ToCharArray()).ToList();
                    bool addBreak = false;

                    foreach (string textSplit in textSplitted)
                    {
                        Run run = CreateRun(openXmlCompositeElement, textSplit);

                        if (addBreak)
                        {
                            run.AppendChild<Break>(new Break());
                        }

                        if (!addBreak)
                        {
                            addBreak = true;
                        }

                        runs.Add(run);
                    }
                }
                else
                {
                    runs.Add(CreateRun(openXmlCompositeElement, content));
                }

                if (openXmlCompositeElement is SdtContentCell)
                {
                    AddRunsToSdtContentCell(openXmlCompositeElement as SdtContentCell, runs);
                }
                else if (openXmlCompositeElement is SdtContentBlock)
                {
                    Paragraph para = CreateParagraph(openXmlCompositeElement, runs);
                    SetSdtContentKeepingPermissionElements(openXmlCompositeElement, para);
                }
                else
                {
                    SetSdtContentKeepingPermissionElements(openXmlCompositeElement, runs);
                }
            }
        }
Hope this helps -- Steve