【问题标题】:Call a UserControl method from another UserControl从另一个 UserControl 调用 UserControl 方法
【发布时间】:2019-09-05 14:19:50
【问题描述】:

我正在尝试从另一个 UserControl 调用来自 UserControl 的方法。我无法跟踪我试图从中调用该方法的 UserControl 来调用该方法。

我正在尝试调用 AddDeal.xaml.cs 中的以下方法

        public void loadDealProducts()
        {
            InfoBox.Information("loadDealProducts called.", "testing");
        }

我正在跟踪 AddDeal UserControl 并尝试使用以下方法调用文件 AddDealProducts.xaml.cs 中的方法 loadDealProducts()

            Window window = null;
            if (sender is Window)
                window = (Window)sender;
            if (window == null)
                window = Window.GetWindow(sender);
            return window;

          (window as AddDeal).loadDealProducts();

但是窗口返回 null 所以我不能调用方法 loadDealProducts。

除了使用 GetWindow 获取 Window 之外,有没有获取 UserControl 的方法?我试过 Window.GetUserControl 和 UserControl.GetUserControl 但没有这样的方法。

sender 是来自 AddDeal.xaml.cs 的 DependencyObject,当我单击 AddDeal.xaml 上的按钮时,如下所示:

<Button Click="BtnAddProducts" CommandParameter="{Binding Path=ProductID}">Add Product</Button>

调用如下:


        private void BtnAddProducts(object sender, RoutedEventArgs e)
        {
            var button = (Button)sender as DependencyObject;
            Window AddProductsDialog = new Window {
                Title = "Add Products to Deal",
                Content = new AddDealProduct(button, productID, false, 0)
            };
            AddProductsDialog.ShowDialog();
        }


如您所见,我正在发送 button,它是 AddDeal.xaml.cs/xaml 上的一个 DependencyObject

当它打开一个新窗口 AddDealProduct 时,它具有 AddDealProduct.xaml(UI 文件)及其 .xaml.cs 代码隐藏文件。在这个文件中,我想从调用 UserControl(AddDeal) 中调用一个函数。

【问题讨论】:

  • 在哪里创建AddDeal 的实例?在什么窗口? AddDealProducts 在哪里?
  • 我有两个文件,AddDeal.xaml 和 AddDealProducts.xaml,这两个文件都有一个 xaml.cs 代码隐藏文件,我在其中进行 c# 编码。这两个文件在 .xaml 文件中都有 ...UI Elements and styles....
  • 我已经编辑了我的问题来解释更多。
  • 既然你的整个窗口都是那个用户控件,我认为它应该是一个窗口而不是一个用户控件。当您从点击或某些此类事件中投射发件人时。 (这里有必要进行一些猜测,因为您解释得不够多)。发件人(可能)将成为一个按钮而不是一个窗口。您必须将发件人强制转换为任何内容,然后 getwindow 。如果发件人在您想要的用户控件中......获取窗口的内容以找到您的用户控件。
  • 你是正确的,我必须得到 UserControl 但它是我想调用的函数,它位于 UserControl 类/文件中。

标签: wpf methods call


【解决方案1】:

好的,我解决了。

我将从源窗口 UserControl 的Button Click 事件中获得的DependencyObject sender 作为参数发送到另一个 UserControl 类。

然后我使用 sender 对象来解析 UserControl 并从不同的 UserControl 类调用其类中的函数。

要调用该函数,我执行以下操作:

AddDealUserControl ownerx2 = FindVisualParent<AddDealUserControl>(sender);
ownerx2.loadDealProducts();

FindVisualParent 辅助类:

        public static T FindVisualParent<T>(DependencyObject child)
     where T : DependencyObject
        {
            // get parent item
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);

            // we’ve reached the end of the tree
            if (parentObject == null) return null;

            // check if the parent matches the type we’re looking for
            T parent = parentObject as T;
            if (parent != null)
            {
                return parent;
            }
            else
            {
                // use recursion to proceed with next level
                return FindVisualParent<T>(parentObject);
            }
        }

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2021-07-26
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多