以动态方式添加点击事件

3
我正在尝试动态添加UI元素,但我遇到了一个问题,我能够添加UI元素,但是我无法将点击事件添加到按钮上。
以下是我的代码:
ParserContext context = new ParserContext();
context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");

string xaml = String.Format(" <StackPanel Orientation='Vertical'>");
xaml = xaml + "<StackPanel Orientation='Horizontal'>";
xaml = xaml + "<Button Margin='5,5,0,0'  Background='AliceBlue' Foreground='DarkBlue' Height='25' VerticalAlignment='Bottom' HorizontalAlignment='Right' Width='82'  Tag='12' Click='btnMy_Click'>";
xaml = xaml + "<StackPanel Orientation='Horizontal'>";
xaml = xaml + "<Image Source='/MotionTest;component/images/open.png' Width='18' Height='18' />";
xaml = xaml + "<TextBlock Text='  Open' VerticalAlignment='Center' FontSize='13' />";
xaml = xaml + "</StackPanel>";
xaml = xaml + "</Button>";
xaml = xaml + "</StackPanel>";
xaml = xaml + "</StackPanel>";

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

我的 onClick 函数:

private void btnMy_Click(object sender, RoutedEventArgs e)
{
    var myValue = ((Button)sender).Tag;
    MessageBox.Show("Here = " + myValue);
}

对于这行代码:

 xaml = xaml + "<Button Margin='5,5,0,0'  Background='AliceBlue' Foreground='DarkBlue' Height='25' VerticalAlignment='Bottom' HorizontalAlignment='Right' Width='82'  Tag='12' Click='btnMy_Click'>";

如果我删除了这个

标签


Click='btnMy_Click'

它会起作用。但如果我添加它,它会显示如下内容: enter image description here 有人知道如何解决吗? 提前感谢。

这个 仅限Xaml的动态事件处理程序 怎么样? - Artavazd Balayan
2
看起来 XamlReader.Parse 不支持事件处理程序。如果支持,至少需要一些上下文信息才能解析事件处理程序方法。您是否有在运行时解析XAML而不是使用数据模板或仅切换UI元素的可见性的特定原因? - Pieter Witvoet
亲爱的Pieter,我的应用程序UI是动态的,它不是固定的。 - Anson Tan
可能是使用XamlReader与Events/Commands的重复问题。 - decPL
@AnsonTan:在运行时解析XAML并不是实现动态UI的唯一方法。您想在运行时进行哪些UI更改?显示和隐藏一些上下文相关的按钮吗? - Pieter Witvoet
1
如果您想要“动态”地创建UI,则只需使用代码直接创建对象。不要为此构造XAML字符串。这完全违背了这里的意图,并且容易出错。 - ManoDestra
1个回答

3

由于错误消息指出需要编译您的XAML文件才能指定事件,而您明确不想这样做,因此似乎是不切实际的(您可以编译临时程序集并加载它,但是然后您无法再卸载它)。

您可以给元素命名:

xaml = xaml + "<Button x:Name='ClickMe'>";

然后,使用一个辅助函数来检索它:
var button = UIHelper.FindChild<Button>(element, "ClickMe");
button.Click += btnMy_Click;

这个帮助函数长这样,我从如何通过名称或类型查找WPF控件?中复制过来:

/// <summary>
/// Finds a Child of a given item in the visual tree. 
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter. 
/// If not matching item can be found, 
/// a null parent is being returned.</returns>
public static T FindChild<T>(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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接