如何创建一个c#属性,以便在设计时订阅另一个WinForm控件?

时间:2022-12-14 15:52:35

I want to create a property in a control that will act as a viewer that will be able to connect to another non-visual control to show its current status. In this example let’s say that the viewer will simply show the status of online or offline.

我想在控件中创建一个属性,该属性将作为查看器,可以连接到另一个非可视控件以显示其当前状态。在本例中,假设查看器将简单地显示联机或脱机状态。

I want to be able to drop a non-visual control, let’s call it a Heater of type IHeater, on the form and then drop a HeaterMonitor. I want to go into the properties of the HeaterMonitor and for the custom Source property see a list of all of the IHeaters currently on the form.

我想要在表单上放置一个非可视控件,我们称它为i加热器类型的加热器,然后放置一个HeaterMonitor。我想进入加热器监视器的属性,关于自定义源属性,请查看当前表单上所有i加热器的列表。

Selecting an instance (Heater1) in the Source property would subscribe HeaterMonitor1 to all the status updates generated by Heater1.

在源属性中选择一个实例(Heater1)将订阅HeaterMonitor1到Heater1生成的所有状态更新。

Is there an existing pattern I can follow as a template?

是否有一个现有的模式可以作为模板来遵循?

If it makes a difference I can use .net 3.5 and higher. I selected data-binding as a tag, but I'm not sure that is correct because this is not a database question. But it does seem similar to a DataGridView selecting a DataSource property.

如果它能产生影响,我可以使用。net 3.5和更高版本。我选择数据绑定作为标记,但我不确定这是否正确,因为这不是一个数据库问题。但是它看起来确实类似于选择DataSource属性的DataGridView。

Edit #1: Based on the comments so far I don't think I emphasized enough what I'm trying to get. I want the property editor to list the eligible IHeater controls on the form. I don't have an issue with creating a regular IHeater property that I can assign at run-time.

编辑#1:基于目前为止的评论,我认为我没有足够强调我想要得到的。我希望属性编辑器在窗体上列出合适的i加热器控件。我在创建一个可以在运行时分配的常规i加热器属性方面没有问题。

1 个解决方案

#1


5  

To have non-UI elements that can be used at design-time in the designer, you can inherit from Component.

要使非ui元素可以在设计时在设计器中使用,可以从组件继承。

using System.ComponentModel;

public interface IHeater
{
    int Temperature { get; set; }
}

public class Heater : Component, IHeater
{
    public int Temperature 
    {
        get;
        set;
    }
}

public class HeaterMonitor:Component
{
    public IHeater Source { get; set; }
}

Then you can use them in design-mode (in component tray):

然后可以在设计模式(组件托盘)中使用:

如何创建一个c#属性,以便在设计时订阅另一个WinForm控件?

And select the source this way:

并通过以下方式选择来源:

如何创建一个c#属性,以便在设计时订阅另一个WinForm控件?

#1


5  

To have non-UI elements that can be used at design-time in the designer, you can inherit from Component.

要使非ui元素可以在设计时在设计器中使用,可以从组件继承。

using System.ComponentModel;

public interface IHeater
{
    int Temperature { get; set; }
}

public class Heater : Component, IHeater
{
    public int Temperature 
    {
        get;
        set;
    }
}

public class HeaterMonitor:Component
{
    public IHeater Source { get; set; }
}

Then you can use them in design-mode (in component tray):

然后可以在设计模式(组件托盘)中使用:

如何创建一个c#属性,以便在设计时订阅另一个WinForm控件?

And select the source this way:

并通过以下方式选择来源:

如何创建一个c#属性,以便在设计时订阅另一个WinForm控件?