动态地向WPF添加事件处理程序

本文关键字:事件处理 程序 添加 WPF 动态 | 更新日期: 2023-09-27 18:10:42

我正在寻找我正在构建的应用程序的帮助。我有一个xml文件被读入一个应用程序。这个xml的结构如下:

 `<Tabs>
  <Tab>
    <Search name="ListSearch" Title="SearchHeader">
      <Label Name="lblSchema"></Label>
      <ComboBox Name="comboxSchema" Visibility="Visible" IsEnabled="True" ItemSource="{Binding AvailableSchema}" SelectedValue="{Binding SelectedSchema}" />
      <ComboBox Name="comboxList" Visibility="Visible" IsEnabled="True" ItemSource="{Binding AvailableList}" SelectedValue="{Binding SelectedList}" />
      <Label Name="lblCriteria"></Label>
      <ComboBox Name="comboxFields" Visibility="Visible" IsEnabled="True" ItemSource="{Binding AvailableFields}" SelectedValue="{Binding SelectedField}" />
      <ComboBox Name="comboxOperator" Visibility="Visible" IsEnabled="True" ItemSource="{Binding Operations}" SelectedValue="{Binding SelectedOperator}" />
      <TextBox Name="txtBoxInputValue" Visibility="Visible" IsEnabled="True" Text="{Binding InputValue}" />
      <CustomControl type="DatePicker"></CustomControl>
      <Button Name="btnAddQueryLine" Content="{Binding TextOnAddQueryButton}" Command="{Binding CamlAddQueryLine}" Action="Publish"></Button>
      <Button Name="btnPasteQueryLine" Content="{Binding TextOnPasteQueryButton}" Command="{Binding CamlPasteQueryLine}" Action="Preview"></Button>
      <Button Name="btnRemoveQueryLine" Content="{Binding TextOnRemoveQueryButton}" Command="{Binding CamlRemoveQueryLine}" Action="UnPublish"></Button>
      <Button Name="btnClearQuery" Content="{Binding TextOnClearQueryButton}" Command="{Binding CamlClearQuery}" Action="UnPreview"></Button>
      <Button Name="btnCamlSearch" Content="{Binding TextOnSearchQueryButton}" Command="{Binding CamlSearch}" Action="Meh"></Button>
      <Button Name="btnCloseSearch" Content="{Binding TextOnCloseQueryButton}" Command="{Binding CloseSearch}" Action="NewMeh"></Button>
    </Search>
  </Tab>
  </Tabs>` 

所以我在xml中读取,并使用这样的方法来添加按钮等功能区:

private void AddButtonToGroup(string header,RibbonGroup group,string command,string action)
    {
         RibbonButton button = new RibbonButton();
         button.Label = header;
         button.Name = action;
         button.Click += new RoutedEventHandler(button_Click);
         group.Items.Add(button);
    }

使用以下事件处理程序:

void button_Click(object sender, RoutedEventArgs e)
    {
        Button clicked = (Button)sender;
        MessageBox.Show("Button Clicked!");
        MessageBox.Show("Performing Action:" + clicked.Name);
    }.

我的问题是,这不是要求-事件处理程序是硬编码的。有没有办法动态地创建事件处理程序?有人能给我指个方向吗?

动态地向WPF添加事件处理程序

您可以创建一个返回Action<object, RoutedEventArgs>的方法:

private Action<object, RoutedEventArgs> MakeButtonClickHandler()
{
   return (object sender, RoutedEventArgs e) =>
      {
         // Put your code here, it will be called when
         // the button is clicked
      };
}

所以MakeButtonClickHandler每次被调用都会返回一个新的匿名函数。然后像这样赋值:

button.Click += new RoutedEventHandler(MakeButtonClickHandler());

另一种方法是内联的,像这样:

button.Click += (object sender, RoutedEventArgs e) =>
    {
        // Put your code here
    };

有关更多信息,请查看MSDN上的匿名函数

您需要定义一组用户将能够执行的操作。然后为它们分配一个名称,并在代码中实现这些操作。

例如,XML格式为:
<Button Content="TextOnAddQueryButton" Command="CamlAddQueryLine"></Button>
在代码中,您可以这样分配事件处理程序:
private void AddButtonToGroup(string header,RibbonGroup group,string command,string action)
{
     RibbonButton button = new RibbonButton();
     button.Tag = command;
     //...
}

按钮的处理程序是:

void button_Click(object sender, RoutedEventArgs e)
{
    Button clicked = (Button)sender;
    switch (clicked.Tag)
    {
        case "CamlAddQueryLine":
            // Call action for CamlAddQueryLine
            break;
    }
}