我正在寻找有关我正在构建的应用程序的帮助.我将一个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>`
Run Code Online (Sandbox Code Playgroud)
所以我读了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);
}
Run Code Online (Sandbox Code Playgroud)
使用以下事件处理程序:
void button_Click(object sender, RoutedEventArgs e)
{
Button clicked = (Button)sender;
MessageBox.Show("Button Clicked!");
MessageBox.Show("Performing Action:" + clicked.Name);
}.
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,这不是要求 - 事件处理程序是硬编码的.有没有办法动态创建事件处理程序?谁能指出我正确的方向?
您可以创建一个返回以下内容的方法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
};
}
Run Code Online (Sandbox Code Playgroud)
因此,MakeButtonClickHandler每次调用它时都会返回一个新的匿名函数.然后像这样分配:
button.Click += new RoutedEventHandler(MakeButtonClickHandler());
Run Code Online (Sandbox Code Playgroud)
完成同样事情的另一种方法是内联,这样:
button.Click += (object sender, RoutedEventArgs e) =>
{
// Put your code here
};
Run Code Online (Sandbox Code Playgroud)