gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:01:47 32 4
gpt4 key购买 nike

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

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com