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

发布于 2024-12-08 12:03:09 字数 2791 浏览 0 评论 0原文

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

    }.

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

I'm looking for help with an app I'm building. I've an xml file being read into an app. This XML is of the following structure:

 `<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>` 

So I read in the xml, and use methods like this to add buttons etc to the ribbon:

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);
    }

with the following event handler:

void button_Click(object sender, RoutedEventArgs e)
    {
        Button clicked = (Button)sender;

        MessageBox.Show("Button Clicked!");
        MessageBox.Show("Performing Action:" + clicked.Name);

    }.

The problem I have, is that this isn't the requirement- the event handler is hard coded. Is there any way to create event handlers dynamically? Can anyone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-12-15 12:03:09

您可以创建一个返回 Action

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 上的匿名函数

You could create a method that returns an 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
      };
}

So MakeButtonClickHandler returns a new anonymous function each time it's called. Then assign it like this:

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

Another way of accomplishing the same thing is to do this inline, like so:

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

For some more information, take a look at Anonymous Functions on MSDN.

辞别 2024-12-15 12:03:09

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

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

You need to define a set of actions that the user will be able to do. Then assign a name to each one of them and implement the actions in code.

For example, the XML would be:

<Button Content="TextOnAddQueryButton" Command="CamlAddQueryLine"></Button>

In code, you would assign the event handler like this:

private void AddButtonToGroup(string header,RibbonGroup group,string command,string action)
{
     RibbonButton button = new RibbonButton();
     button.Tag = command;
     //...
}

And the handler for the button would be:

void button_Click(object sender, RoutedEventArgs e)
{
    Button clicked = (Button)sender;

    switch (clicked.Tag)
    {
        case "CamlAddQueryLine":
            // Call action for CamlAddQueryLine
            break;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文