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

时间:2011-10-05 17:45:33

标签: 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);

    }.

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

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:2)

您需要定义一组用户可以执行的操作。然后为每个名称分配一个名称并在代码中实现操作。

例如,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;
    }
}