2011-10-05 81 views
2

我正在尋找我正在構建的應用程序的幫助。我有一個XML文件被讀入應用程序。這個XML是以下結構:動態添加事件處理程序到WPF

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

    }. 

我的問題是,這不是要求 - 事件處理程序是硬編碼的。有沒有辦法動態創建事件處理程序?任何人都可以將我指向正確的方向嗎?

+0

用戶可以將哪種操作分配給按鈕?你需要以某種方式限制它。動態創建代碼可能非常複雜。 – alf

+0

這就是問題所在。潛在地,這個動作可以運行一個搜索,刪除一些東西,打開一個彈出窗口,打開一個新標籤,取消一個搜索,或者其他一些事情。 – scarecrow198504

回答

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

+0

嘿@甜甜圈,我明白你在這裏做什麼,但這仍然不是動態的,是嗎? MakeButtonClickHandler仍然是一種硬編碼方法。我需要找到一種方法來從我放入XML中的東西中即時創建事件處理程序。這甚至有可能嗎? – scarecrow198504

+0

@ scarecrow198504你可以舉一些你希望如何工作的具體例子嗎?我猜你的行爲是基於你發佈的AddToButtonGroup方法的'command'和'action'參數。這是否正確,以及您希望這些參數如何工作? – Donut

相關問題