c# - 向 WPF 动态添加事件处理程序

标签 c# wpf ribbon

我正在寻求有关我正在构建的应用程序的帮助。我有一个正在读入应用程序的 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);

    }.

我遇到的问题是,这不是要求 - 事件处理程序是硬编码的。有没有办法动态创建事件处理程序?谁能指出我正确的方向?

最佳答案

您可以创建一个返回 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
    };

有关更多信息,请查看 Anonymous Functions on MSDN .

关于c# - 向 WPF 动态添加事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7665401/

相关文章:

c# - 为什么我不能在 XAML 中定义 ResourceDictionary 并自行实例化它?

.NET VSTO - Excel 功能区选项卡标签为空白,但仅在 Excel 的第一个实例上

c# - 具有多个打开的工作簿的自定义任务 Pane

c# - 使用 Moq 模拟 IDataRecord

c# - 无法使用 EntityFramework 教程添加迁移

c# - 为什么我的析构函数从不运行?

c# - 如何将存储的MP3文件数据(byte [])转换为float [] Unity可以用作AudioSource-Data?

wpf - 我的数据触发绑定(bind)有什么问题?

c# - WPF DataGrid 行上的 ContextMenu - 事件不会触发