2011-03-10 32 views
0

我在buttonclick(xmlparsingbutton)事件期間創建了一個動態按鈕(xmlparsingbuildbutton)。xml解析中的動態按鈕事件

我做了一個xml解析...一旦每個xml目標被解析,他們將創建一個單獨的複選框。我需要的是:創建一個動態按鈕事件(xmlparsingbuildbutton)。在此動態按鈕事件期間,對於每個選中的複選框,它們將通過在文本文件中寫入新行來附加文本文件。這讓我感到困惑,因爲現在我有一個嵌入xmlparsingbutton事件的foreachloop,創建所有這些複選框。

現在,我編寫它的方式是,每當我檢查複選框,我的文件事件追加將不會觸發,因此我創建了這個「xmlparsingbuildbutton」來幫助觸發事件。從我的理解,通常我會硬編碼buttonclickevent,但在這種情況下,target值總是在foreach循環中更改,因此僅對buttonclickevent進行硬編碼是不對的。

所以我的問題是我如何讓這個按鈕事件在xmlparsing事件按鈕的foreach循環中?請澄清我的疑惑。非常感謝!

這裏是我的代碼:

private void xmlparsingButton_Click(object sender, RoutedEventArgs e) 
{ 
    XDocument xmlDoc = XDocument.Load(@"C:\Build.xml"); 
    var abc = from target in xmlDoc.Descendants("target") 
       select (string)target.Attribute("if"); 
    ColumnDefinition gridCol1 = new ColumnDefinition(); 
    gridCol1.Width = new GridLength(300); 
    ColumnDefinition gridCol2 = new ColumnDefinition(); 
    gridCol2.Width = new GridLength(300); 
    ColumnDefinition gridCol3 = new ColumnDefinition(); 
    gridCol3.Width = new GridLength(300); 
    ColumnDefinition gridCol4 = new ColumnDefinition(); 
    gridCol4.Width = new GridLength(300); 
    tab4grid.ColumnDefinitions.Add(gridCol1); 
    tab4grid.ColumnDefinitions.Add(gridCol2); 
    tab4grid.ColumnDefinitions.Add(gridCol3); 
    tab4grid.ColumnDefinitions.Add(gridCol4); 
    RowDefinition gridRow1 = new RowDefinition(); 
    gridRow1.Height = new GridLength(50); 
    RowDefinition gridRow2 = new RowDefinition(); 
    gridRow2.Height = new GridLength(50); 
    RowDefinition gridRow3 = new RowDefinition(); 
    gridRow3.Height = new GridLength(50); 
    RowDefinition gridRow4 = new RowDefinition(); 
    gridRow4.Height = new GridLength(50); 
    RowDefinition gridRow5 = new RowDefinition(); 
    gridRow5.Height = new GridLength(50); 
    RowDefinition gridRow6 = new RowDefinition(); 
    gridRow6.Height = new GridLength(50); 
    RowDefinition gridRow7 = new RowDefinition(); 
    gridRow7.Height = new GridLength(50); 
    RowDefinition gridRow8 = new RowDefinition(); 
    gridRow8.Height = new GridLength(50); 
    RowDefinition gridRow9 = new RowDefinition(); 
    gridRow9.Height = new GridLength(50); 
    RowDefinition gridRow10 = new RowDefinition(); 
    gridRow10.Height = new GridLength(50); 
    RowDefinition gridRow11 = new RowDefinition(); 
    gridRow11.Height = new GridLength(50); 
    RowDefinition gridRow12 = new RowDefinition(); 
    gridRow12.Height = new GridLength(50); 
    tab4grid.RowDefinitions.Add(gridRow1); 
    tab4grid.RowDefinitions.Add(gridRow2); 
    tab4grid.RowDefinitions.Add(gridRow3); 
    tab4grid.RowDefinitions.Add(gridRow4); 
    tab4grid.RowDefinitions.Add(gridRow5); 
    tab4grid.RowDefinitions.Add(gridRow6); 
    tab4grid.RowDefinitions.Add(gridRow7); 
    tab4grid.RowDefinitions.Add(gridRow8); 
    tab4grid.RowDefinitions.Add(gridRow9); 
    tab4grid.RowDefinitions.Add(gridRow10); 
    tab4grid.RowDefinitions.Add(gridRow11); 
    tab4grid.RowDefinitions.Add(gridRow12); 
    int i = 0; 
    int j = 1; 
    System.Windows.Controls.Button XmlparsingbuildButton = new System.Windows.Controls.Button(); 
    Grid.SetColumn(XmlparsingbuildButton, 4); 
    Grid.SetRow(XmlparsingbuildButton, 12); 
    XmlparsingbuildButton.Height = 23; 
    XmlparsingbuildButton.Width = 51; 
    XmlparsingbuildButton.Content = "Build"; 
    tab4grid.Children.Add(XmlparsingbuildButton); 
    foreach(string target in abc) 
    { 
     if (target != null && !Dictionarycheck.ContainsKey(target)) 
     { 
      System.Windows.Controls.CheckBox chk = new System.Windows.Controls.CheckBox(); 
      chk.Name = target+"CheckBox"; 
      chk.Content = target; 
      Grid.SetColumn(chk, i); //sets column 
      Grid.SetRow(chk, j); //sets row      
      tab4grid.Children.Add(chk); //adds the control 
      Tabitem5.Visibility = Visibility.Visible; 
      i++; 
      if (i == 4) 
      { 
       j++; 
       i = 0; 
      } 

      if (chk.IsChecked == true) 
      { 
       using (var writer = File.AppendText(@"c:\testing.txt")) 
       { 
        writer.WriteLine(target); 
       } 

      } 
     } 
    } 
    Tabitem5.Visibility = Visibility.Visible; 
    Tabcontrol1.SelectedIndex = 4; 


} 

回答

0

我不得不承認,我也理解你的問題的問題。如果我得到它的權利,你要做到以下幾點:

  • 解析一個XML文檔( 「C:\在build.xml」)與(大約)格式如下:
 
    targets> 
     target if="aaa" /> 
     target if="bbb" /> 
    /targets> 
  • 爲每個目標標籤的複選框與如 - 屬性設置
  • 檢查包含要使用
  • 點擊一個按鈕,選中的字符串追加到一個文本文件中的字符串(「C的複選框:\測試。文本「)

如果這是正確的,那麼我認爲你的解決方案不會這樣做。我不確定是否可以理解你在代碼中想要做什麼。您設置組合框的方式在您使用作家時永遠不會被檢查。

如何:

  • 使用一個StackPanel(stackPanel1),而不是一個網格。這樣你就不必創建所有的行定義。 (好吧,我不得不承認,我不知道所有這些列的)
  • 創建一個簡單的邏輯,一個按鈕,選中的組合框追加到該文件

代碼:

private void xmlparsingButton_Click(object sender, RoutedEventArgs e) 
{ 
    stackPanel1.Children.Clear(); 

    XDocument xmlDoc = XDocument.Load(@"C:\Build.xml"); 

    var abc = from target in xmlDoc.Descendants("target") 
      select (string) target.Attribute("if"); 

    foreach (string target in abc) 
    { 
    if (target != null) 
    { 
     System.Windows.Controls.CheckBox chk = new System.Windows.Controls.CheckBox(); 
     chk.Name = target + "CheckBox"; 
     chk.Content = target; 
     chk.Tag = target; 
     stackPanel1.Children.Add(chk); 
    } 
    } 
} 

private void btnAppendToTextFile_Click(object sender, RoutedEventArgs e) 
{ 
    using (var writer = File.AppendText(@"c:\testing.txt")) 
    { 
    foreach (var child in stackPanel1.Children) 
    { 
     if ((child is CheckBox) && ((CheckBox) child).IsChecked.Value) 
     { 
     writer.WriteLine(((CheckBox)child).Tag); 
     } 
    } 
    } 
} 

我希望這可以幫助你解決你的問題。

+0

哦,謝謝gimmo ..那就是我一直在尋找的東西。非常感謝 – jeremychan 2011-03-11 03:24:42