Warm tip: This article is reproduced from serverfault.com, please click

C# Autocad UserControl WPF change text box from another class

发布于 2018-10-21 21:42:28

This is a classic, Usercontrol (WPF) text box that needs to be updated from another class. This is an Autocad plug-in. I will only show what is needed.

I tried to do the following but something doesn't seem to be working here: Changing text box from another class

The user control code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace SimpleWpfPalette
{
    public partial class PaletteUserControl : UserControl
    {

        char quote = '"';

        public PaletteUserControl()
        {
            InitializeComponent();
        }

        private void processButton_Click(object sender, RoutedEventArgs e)
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            if (doc != null)
                doc.SendStringToExecute("UAIF " + quote + totalSheetsBox.Text + quote + " " + quote + originatorBox.Text + quote + " " + quote + approverBox.Text + quote +
                   " " + quote + identBox.Text + quote + " " + quote + dateBox.Text + quote + " " + quote + germanBox.Text + quote + " " + quote + weightBox.Text + quote +
                   " " + quote + drawingnbrBox.Text + quote + " " + quote + indexBox.Text + quote + " " + quote + englishBox.Text + quote + " " + quote + frenchBox.Text + quote
                   + "\n", false, false, false);
        }

        private void nameBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void M10Button_Click(object sender, RoutedEventArgs e)
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            if (doc != null)
                doc.SendStringToExecute("m10file" + "\n", false, false, false);
        }

        public void setidentBox (string text)
        {
           identBox.Text = text;       
        }

    }
}

Basically I am trying to call the following in another class:

public void setidentBox (string text)
{
   identBox.Text = text;       
}

Now in my class which I am going to condense greatly, we have the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using System.IO;
using System.Drawing;
using System.Windows.Forms.Integration;


namespace SimpleWpfPalette
{
    public class Commands
    {
        static CustomPalette palette;

[CommandMethod("m10file")]
        public void ReadingM10File()
        {

            Document doc =
            Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            string fileDirectory = "C:\\Users\\Dustin\\source\\repos\\ReadInM10File";

            string[] stringArray = new string[15];

            M10Class[] M10DataInstance = new M10Class[15];

            for (int i = 0; i < M10DataInstance.Length; i++)
            {
                M10DataInstance[i] = new M10Class();
            }

            OpenFileDialog ofd = new OpenFileDialog("Select M10 File", null, "m10", "M10FileToLink", OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder);

            ofd.ShowDialog();

            var lines = File.ReadAllLines(ofd.Filename, Encoding.Default);

            int counter = 0;
            foreach (var line in lines)
            {
                M10DataInstance[counter].idNum = line.Remove(3);
                M10DataInstance[counter].dataString = line.Remove(0, 6);
                counter++;

            }

                for (int i = 0; i < M10DataInstance.Length; i++)
                {
                    ed.WriteMessage(M10DataInstance[i].idNum + " " + M10DataInstance[i].dataString);
                }

            // PaletteUserControl puc = new PaletteUserControl();

            // puc.setidentBox("test");

        }

So you can see my last two lines of code, I was creating a new instance of the PaletteUserControl which I know will not update my current one.

Basically, how can I simply expose the existing PaletteUserControl to simply allow me to call the setidentBox(string) function?

Questioner
Busta
Viewed
0
gileCAD 2018-10-22 14:10:05

In the CustomPalette class, you add a public property which exposes the PaletteUserControl added to the palette set:

public class CustomPalette : PaletteSet
{
    private PaletteUserControl userControl;

    public PaletteUserControl PaletteUserControl => userControl;

    public CustomPalette() : base("CustomPalette")
    {
        Style = PaletteSetStyles.ShowAutoHideButton |
                PaletteSetStyles.ShowCloseButton |
                PaletteSetStyles.ShowPropertiesMenu;

        userControl = new PaletteUserControl();
        AddVisual("PaletteUserControl", userControl);
    }
}

In the PaletteUserControl class, you define the SetIdentBox method:

public partial class PaletteUserControl : UserControl
{
    public PaletteUserControl()
    {
        InitializeComponent();
    }

    public void SetIdentBox(string text)
    {
        identBox.Text = text;
    }
}

In the Commands class, you can call the palette.PaletteUserControl.SetIdentBox() method:

palette.PaletteUserControl.SetIdentBox("test");

Anyway, while using WPF you should have a look at the WPF data bindings features instead.