【问题标题】:Call a page function from usercontrol code从用户控件代码调用页面函数
【发布时间】:2021-05-21 21:04:10
【问题描述】:

我正在开发一个 C# 和 WPF 应用程序。 我有一个带有用户控件“CreateItem”的 WPF 页面“HomeView”。在用户控件中单击一个按钮(相关函数为Close())后,我必须使用户控件可见性折叠并在页面代码中调用函数“ReloadAnalytics”。

namespace iMP
{
    public partial class CreateItem : UserControl
    {

    public CreateItem()
    {
        InitializeComponent();
    }

    private void Close(object sender, RoutedEventArgs e)
    {
        HomeView objHomeView= new HomeView();
        objHomeView.CreateNewItemDisplayGrid.Visibility = Visibility.Collapsed;
        objHomeView.InitializeAnalyticsOverview();
    }
}

当我调用这个函数时,按下按钮,什么都没有发生。 (CreateNewItemDisplayGridHomeView 中 UserControl 的名称) (HomeView命名空间是iMP.Views,公共部分类是HomeView : Page

【问题讨论】:

  • 据我了解,我认为您需要从CreateItem 用户控件调用一个事件,然后在您的HomeView 页面中订阅此事件,该页面实际上执行现在在@ 中的代码987654330@.

标签: c# wpf function class


【解决方案1】:

按照下面的结构。我在usercontrol 中定义了一个事件,并在主窗体中使用了该事件。

尝试将代码与您的代码匹配

在用户控件中

public partial class CreateItem : UserControl
{
    public event ClickEventHandler CloseButtonClick;
    public delegate void ClickEventHandler(object Sender, RoutedEventArgs e);
    public CreateItem()
    {
       InitializeComponent();
    }

    private void Close(object sender, RoutedEventArgs e)
    {
       if (CloseButtonClick != null)
       {
           CloseButtonClick(this, e);
       }
    }
}

在主视图中

public partial class HomeView : Page
{
   public HomeView()
   {
      InitializeComponent();
      CreateItem userControl = new CreateItem();
      this.Content = userControl;
      userControl.CloseButtonClick += UserControl_CloseButtonClick;
   }

   private void UserControl_CloseButtonClick(object Sender, RoutedEventArgs e)
   {
       ///...................................
       this.CreateNewItemDisplayGrid.Visibility = Visibility.Collapsed;
       ///.......................
   }
}

【讨论】:

    猜你喜欢
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多