xml解析中的动态按钮事件

发布于 2024-10-21 07:15:59 字数 4386 浏览 0 评论 0原文

我在按钮单击(xmlparsingbutton)事件期间创建一个动态按钮(xmlparsingbuildbutton)。

我进行了 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;


}

i create a dynamic button(xmlparsingbuildbutton) during a buttonclick(xmlparsingbutton) event.

I did a xml parsing...once each xml target is parsed, they will create an individual checkbox. What i need is this: Create a dynamic button event(xmlparsingbuildbutton). During this dynamic button event, for every checkbox that is checked, they will append a text file by writing new lines into the text file. This gets confusing to me because right now i have a foreachloop embedded in the xmlparsingbutton event that creates all these checkboxes.

Right now, I've coded it in such a way that whenever i check the checkboxes, my append of file event will not fire therefore I created this "xmlparsingbuildbutton" to help fire the event.From my understanding, usually i would hard code buttonclickevent, however in this case the target value always change in the foreach loop and therefore it is not right to just merely hardcode the buttonclickevent.

So my question is how do i have this button event to be inside the foreach loop of the xmlparsing event button? Please clarify my doubts. Many thanks!

Here is my code:

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;


}

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

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

发布评论

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

评论(1

许仙没带伞 2024-10-28 07:15:59

我必须承认,我很难理解你的问题。如果我做对了,您将尝试执行以下操作:

  • 使用(大致)此格式解析 Xml 文档(“C:\Build.xml”):
    targets>
      target if="aaa" />
      target if="bbb" />
   /targets>
  • 为每个带有 if- 属性集的目标标记创建一个复选框
  • 检查包含要使用的字符串的复选框
  • 单击按钮将选中的字符串附加到文本文件(“c:\testing.txt”)

如果这是正确的,那么我认为您的解决方案不会这样做。我不确定我是否能理解您在代码中尝试执行的操作。您设置组合框的方式在您使用编写器时永远不会检查它们。

怎么样:

  • 使用 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);
      }
    }
  }
}

我希望这可以帮助您解决问题。

I have to admit, I have problems understanding your problem. If I get it right, you're trying to do the following:

  • Parse a Xml document ("C:\Build.xml") with (roughly) this format:
    targets>
      target if="aaa" />
      target if="bbb" />
   /targets>
  • Create a CheckBox for each target tag with if- attribute set
  • Check the CheckBox containing the string you want to use
  • Click a button to append the checked strings to a text file ("c:\testing.txt")

If this is correct, then I think your solution won't do that. I not sure if I can understand what you are trying to do in your code. The way you set up the comboboxes they will never be checked the time you use your writer.

How about this:

  • Use a StackPanel (stackPanel1) instead of a grid. That way you don't have to create all the row definitions. (Ok, I have to admit, I don't know what all these columns are for)
  • Create a simple logic for a button to append the checked ComboBoxes to the file

code:

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

I hope this helps you with your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文