C#多态调用网格的刷新事件

Call Refresh event of grid Polymorphically C#

我在 WPF 中有一个主要 window,它包含:

  1. 具有不同选项卡的选项卡控件。每个选项卡中都有一个不同的控件,并包含一个数据网格。
  2. 一个框架控件 - 它也有不同的控件分别用于数据输入。
  3. 一个刷新按钮(尚未实现)

我已经成功实现了用于数据输入的选项卡控件和框架,但问题是在切换选项卡之前无法刷新选项卡控件。我想在 main-window 上有一个中央刷新按钮(我提到过的那个)。 谁能指导我该怎么做?

而且由于tab的当前对象类型只会在运行时决定,所以是多态性吗?

您可以为所有用户控件使用一个界面:

public interface IRefreshAble
{
    void Refresh();
}

public interface ICanDeleteItem
{
    void Delete(/*parameters omitted*/);
    bool CanDelete { get; }

}

public interface IHoldATypedItem ///sorry for bad name
{
    Type TheType { get; }
}

然后你通过用户控件实现这个接口:

public class TheUserControl : UserControl, IRefreshAble, ICanDeleteItem, IHoldATypedItem 
{
     public void Refresh()
     {
         //Your refreshcode
     }

     public bool CanDelete {get { /*code that indicates if an item can be deleted*/ } }


     public void Delete(/*parameters ommited*/)
     {
          if(CanDelete)
          {
             //Delete Item
          }
     }

     public Type TheType { get { return typeOf(Employee); } }
     // otherstuff
}

现在您可以将所有 UserControl(例如)放入 List 中,然后执行以下操作:

foreach(IRefreshAble item in theList)
{
    item.Refresh();
}

如果您的所有用户控件通用的 Refresh() 方法不止于此,您只需扩展该成员的接口并获得所需的多态性即可。