Telerik UI WPF – How Can I Add a DocumentVariableField to a Table Cell Programmatically

We are using Telerik UI for WPF and are creating a table which then gets inserted into a RadRichTextBox. We need to add a DocumentVariableField to one of the cells, can anyone please advise.

        public void AddAddress()
        {
            string addrName = " ";
            string[] rowName = new string[] { "Job Title", "Employer", "Address", "Tel.", "Tel. Dir.", "Mobile", "Email", "Website", "Fax", "Details", "Address", "Tel.", "Mobile", "Email", "Details" };
            if(_selelement != null)
            {
                addrName = _selelement.GetAttribute("title");
            }
            RadDocument document = new RadDocument();
            Section section = new Section();

            TableWidthUnit w1 = new TableWidthUnit(100);
            TableWidthUnit w2 = new TableWidthUnit(520);

            Table table = new Table();
            table.StyleName = RadDocumentDefaultStyles.DefaultTableGridStyleName;

            TableRow row1 = new TableRow();
            TableCell cell1 = new TableCell();
            cell1.Background = Color.FromRgb(174, 255, 190);
            cell1.PreferredWidth = w1;
            cell1.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
            Paragraph p1 = new Paragraph();
            Span s1 = new Span();
            s1.FontWeight = FontWeights.Bold;
            s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
            s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
            s1.Text = "Name";
            p1.Inlines.Add(s1);
            cell1.Blocks.Add(p1);
            row1.Cells.Add(cell1);

            TableCell cell2 = new TableCell();
            cell2.Background = Color.FromRgb(174, 255, 190);
            cell2.PreferredWidth = w2;
            cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
            Paragraph p2 = new Paragraph();
            Span s2 = new Span();
            s2.FontWeight = FontWeights.Bold;
            s2.FontFamily = new FontFamily(CGlobals.docu_default_font);
            s2.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
            s2.Text = addrName;  // This needs to contain the DocumentVariable
            p2.Inlines.Add(s2);
            cell2.Blocks.Add(p2);
            row1.Cells.Add(cell2);
            table.Rows.Add(row1);

            int rowCount = 0;
            foreach(string rname in rowName)
            {
                rowCount++;
                row1 = new TableRow();
                cell1 = new TableCell();
                p1 = new Paragraph();
                if (rowCount < 11)
                {
                    cell1.Background = Color.FromRgb(255, 229, 153);
                    p1.Background = Color.FromRgb(255, 229, 153);
                }
                else
                {
                    cell1.Background = Color.FromRgb(196, 255, 255);
                    p1.Background = Color.FromRgb(196, 255, 255);
                }
                cell1.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                s1 = new Span();
                s1.FontWeight = FontWeights.Bold;
                s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                s1.Text = rname;
                p1.Inlines.Add(s1);
                cell1.Blocks.Add(p1);
                row1.Cells.Add(cell1);

                cell2 = new TableCell();
                cell2.Background = Color.FromRgb(255, 255, 255);
                cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                p2 = new Paragraph();
                s2 = new Span();
                s2.FontFamily = new FontFamily(CGlobals.docu_default_font);
                s2.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                s2.Text = " ";
                p2.Inlines.Add(s2);
                cell2.Blocks.Add(p2);
                row1.Cells.Add(cell2);
                table.Rows.Add(row1);
            }

            section.Blocks.Add(new Paragraph());
            section.Blocks.Add(table);
            section.Blocks.Add(new Paragraph());
            document.Sections.Add(section);

            doc_text.Document = document;

            // Add a DocumentVariable and assign the value to it
            // This needs to be changed so the variable can be added to Row 1, Cell 2 in the table created above
            doc_text.Document.DocumentVariables.Add("1001", addrName);
            DocumentVariableField docVariable = new DocumentVariableField() { VariableName = "1001"  };
           
            doc_text.InsertField(docVariable, FieldDisplayMode.Result);

//            ChangeAddress(addrName);
        }

A variable needs to be used so it can be updated when another field in the UI is amended.

In the above code, the DocumentVariableField is added to the document and we are able to update by:

public void ChangeAddress(string address)
{
    doc_text.Document.DocumentVariables["1001"] = address;
    var field = doc_text.Document.EnumerateChildrenOfType<FieldRangeStart>().Where(x => x.Field.FieldTypeName == "DOCVARIABLE"
    && ((DocumentVariableField)x.Field).VariableName == "1001").FirstOrDefault();

    if (field != null)
    {
        doc_text.UpdateField(field);
    }
}

I have tried using Tags but they do not persist when the contents of the RadRichTextBox are saved to an RTF.

Thank you

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật