【问题标题】:Setting the value in OpenXml checkbox word2013在 OpenXml 复选框 word2013 中设置值
【发布时间】:2015-08-19 16:39:53
【问题描述】:

所以我有一个正在通过 WPF(C#) 应用程序编辑的文档。 我已成功编辑纯文本内容控件,但现在我卡住了选中/取消选中表单中的复选框。

我成功找到了复选框并设置了值并保存了文档,但是当我打开它时,设置为 true 的复选框从未在 word 文档中被选中。

这是我用来操作复选框的代码。 注意:我在标签级别访问复选框,因此 field.parent.parent

private static void SetCheckBox(OpenXmlElement field, bool isChecked)
{
    var checkBox = field.Parent.Parent.Descendants<SdtContentCheckBox>().ToList();
    foreach (var check in checkBox)
    {
        if (isChecked)
        {
            check.Checked.Val = OnOffValues.True;
        }
        else
        {
            check.Checked.Val = OnOffValues.False;
        }
        MessageBox.Show(check.Checked.Val);
    }
}

当我在 MessageBox 中显示值时,它们显示 0/1 表示真/假。所以它们实际上正在设置中。

我这样做正确吗?

【问题讨论】:

    标签: c# wpf checkbox openxml


    【解决方案1】:

    看来不仅必须设置复选框的Checked 值,还必须更改Text 值。

    所以我最近的代码也有一些改动,但它改变了复选框的麻烦方面。

    代码:

    private static void SetCheckBox(OpenXmlElement field, bool isChecked)
    {
        if (isChecked)
        {
            field.Parent.Parent.FirstChild.GetFirstChild<SdtContentCheckBox>().Checked.Val = OnOffValues.True;
            field.Parent.Parent.Descendants<Run>().First().GetFirstChild<Text>().Text = "☒";
        }
        else
        {
            field.Parent.Parent.FirstChild.GetFirstChild<SdtContentCheckBox>().Checked.Val = OnOffValues.False;
            field.Parent.Parent.Descendants<Run>().First().GetFirstChild<Text>().Text = "☐";
        }
    }
    

    浓缩:

    private static void SetCheckBox(OpenXmlElement field, bool isChecked)
    {
        field.Parent.Parent.FirstChild.GetFirstChild<SdtContentCheckBox>().Checked.Val = isChecked ? OnOffValues.True : OnOffValues.False;
        field.Parent.Parent.Descendants<Run>().First().GetFirstChild<Text>().Text = isChecked ? "☒" : "☐";
    }
    

    【讨论】:

    • 这能解决你的问题吗?
    • @MaximePorté 是的
    • 如果此代码解决了问题,请将其标记为答案。
    【解决方案2】:

    解决问题的另一个版本的代码:

        private void ResetFile(string filePath)
        {
            using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))
            {
                try
                {
                    string uncheckValue = "☐";
                    string checkValue = "☒";
    
                    foreach (SdtContentCheckBox ctrl in doc.MainDocumentPart.Document.Body.Descendants<SdtContentCheckBox>())
                    {
                        if (ctrl.Checked.Val == OnOffValues.One)
                        {
                            ctrl.Checked.Val = OnOffValues.Zero;
                            if (ctrl.Parent.Parent.Descendants<SdtContentRun>().ToList().Count > 0)
                            {
                                SdtContentRun text = (SdtContentRun)ctrl.Parent.Parent.Descendants<SdtContentRun>().ToList()[0];
                                text.InnerXml = text.InnerXml.Replace(checkValue, uncheckValue);
                            }
                        }
                    }
    
                    doc.MainDocumentPart.Document.Save();
                }
                catch { }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多