C# Autocad UserControl WPF 从另一个更改文本框 class

C# Autocad UserControl WPF change text box from another class

这是一个 classic,用户控件 (WPF) 文本框,需要从另一个 class 更新。这是一个 Autocad 插件。我只会展示需要的东西。

我尝试执行以下操作,但此处似乎不起作用: Changing text box from another class

用户控制码:

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;       
        }

    }
}

基本上我试图在另一个 class 中调用以下内容:

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

现在在我要大大压缩的 class 中,我们有以下内容:

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");

        }

所以你可以看到我的最后两行代码,我正在创建一个新的 PaletteUserControl 实例,我知道它不会更新我当前的实例。

基本上,我怎样才能简单地公开现有的 PaletteUserControl 以允许我调用 setidentBox(string) 函数?

在 CustomPalette class 中,您添加一个 public 属性,它公开添加到调色板集中的 PaletteUserControl:

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);
    }
}

在 PaletteUserControl class 中,您定义了 SetIdentBox 方法:

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

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

在命令 class 中,您可以调用 palette.PaletteUserControl.SetIdentBox() 方法:

palette.PaletteUserControl.SetIdentBox("test");

无论如何,在使用 WPF 时,您应该看看 WPF 数据绑定功能。