【问题标题】:Set event to button when created via XAML string通过 XAML 字符串创建时将事件设置为按钮
【发布时间】:2018-07-05 11:57:35
【问题描述】:

我得到以下代码,将是一个动态生成的网格:

string xaml = "<Grid Margin='8 8 0 0' Width='175'>" +
                "<Grid.RowDefinitions>" +
                    "<RowDefinition Height='115'/>" +
                    "<RowDefinition Height='*'/>" +
                    "<RowDefinition Height='Auto'/>" +
                "</Grid.RowDefinitions>" +
                "<Image Source='C:/Users/SaFteiNZz/Curro/yo2.png' Height='115' Width='171' Stretch='Uniform'/>" +
                "<Button x:Name='BtnEditAc' Grid.Row='0' Style='{StaticResource MaterialDesignFloatingActionMiniAccentButton}' HorizontalAlignment='Right' VerticalAlignment='Bottom' Margin='0,0,16,-20'>" +
                    "<materialDesign:PackIcon Kind='AccountSettingsVariant' Width='30' Height='30' Margin='0,0,0,5'/>" +
                "</Button>" +
                "<StackPanel Grid.Row='1' Margin='8,24,8,8'>" +
                    "<TextBlock TextWrapping='Wrap' FontWeight='Bold'>Pablo Antonio Hernandez Hernandez</TextBlock>" +
                    "<TextBlock TextWrapping='Wrap' VerticalAlignment='Center'>46080696N</TextBlock>" +
                "</StackPanel>" +
                "<Border BorderBrush='White' BorderThickness='1' CornerRadius='0' Grid.Row='0' Grid.RowSpan='3'>" +
                    "<Border.Effect>" +
                        "<DropShadowEffect BlurRadius='10' Direction='-90' Color='Black' ShadowDepth='0'/>" +
                    "</Border.Effect>" +
                "</Border>" +
            "</Grid>";

UIElement element = (UIElement)XamlReader.Parse(xaml, context);
empDisplay.Children.Add(element);

就像代码工作一样。

但我想为 按钮 (BtnEditAc) 设置一个点击事件,如果我直接在 XAML 字符串中执行此操作会引发错误(无法从文本 BtnEditAc_Click 或类似内容创建点击)。


有没有办法为那个按钮设置点击事件?或者以某种方式连接到一个函数?

我将使用 CLR 对象编码而不是 XamlReader 来实现它,但首先我想知道是否有解决方案。

希望你知道我做错了什么。

【问题讨论】:

  • 您可以导航树(或按名称查找按钮)并添加委托以单击事件。我认为您不能直接从字符串中执行此操作...您将事件处理程序的代码放在哪里?
  • @Babbillumpa 事件处理程序就在包含它的函数的下方,在同一个类中。

标签: c# wpf xaml controls dynamically-generated


【解决方案1】:

让我们看看这个解决方案是否可行:

//Here you have the grid
Grid element = (Grid)XamlReader.Parse(xaml, context);
//Now try to get the button
Button btn = (Button)element.FindName("BtnEditAc");
if(btn != null)
{
    btn.Click  += new RoutedEventHandler(OnBtnEditAcClick);
}

其中OnBtnEditAcClick 是事件处理程序。 然后将该元素添加到您的主视图中:

empDisplay.Children.Add(element);

我希望它会有所帮助。

【讨论】:

  • 没什么,看看另一个答案,他添加了您不知道控件名称并在可视化树中找到它的情况。
【解决方案2】:

您可以从 xml 而非 Click 事件中解析命令绑定:

<Button x:Name='BtnEditAc' Command='{Binding MyCommand}' Grid.Row='0' ...

如果由于某种原因你不想使用命令,你可以在代码中手动添加点击事件,只要你能在你刚刚解析的xaml元素中找到Button。例如,如果您知道序列化 xaml 中的根元素将是 Grid

Grid rootElement = (Grid)XamlReader.Parse(xaml, context);
grdMain.Children.Add(rootElement);
Button btn = (Button)rootElement.FindName("BtnEditAc");
btn.Click += btn_Click;

或者:

UIElement elem = (UIElement)XamlReader.Parse(xaml, context);
Button b =(Button) LogicalTreeHelper.FindLogicalNode(elem, "BtnEditAc");

或者,如果您不知道根元素的类型,您可以使用this code 按名称和类型查找它:

    public static T FindChild<T>(this DependencyObject parent, string childName)
       where T : DependencyObject
    {
        // Confirm parent and childName are valid. 
        if (parent == null) return null;

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            // If the child is not of the request child type child
            T childType = child as T;
            if (childType == null)
            {
                // recursively drill down the tree
                foundChild = FindChild<T>(child, childName);

                // If the child is found, break so we do not overwrite the found child. 
                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                // If the child's name is set for search
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    // if the child's name is of the request name
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                // child element found.
                foundChild = (T)child;
                break;
            }
        }

        return foundChild;
    }

或者如果您不知道按钮的名称,例如,您可以使用this extension method 获取 Button 类型的第一个子元素(或实现将返回给定类型的所有子元素的方法):

    public static T GetChildOfType<T>(this DependencyObject depObj) where T : DependencyObject
    {
        if (depObj == null) return null;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var result = (child as T) ?? GetChildOfType<T>(child);
            if (result != null) return result;
        }
        return null;
    }

如果你想在xaml中设置你的事件处理程序并手动解析它,我不知道它是否可以做到。

【讨论】:

  • 是的,命令绑定是我的替代方案之一,在这种情况下,“grid.FindName()”最终成为最短和最简单的方法。感谢您展示更多替代方案和案例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多