我在buttonclick(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;
}
答案 0 :(得分:0)
我必须承认,我在理解你的问题时遇到了问题。如果我做对了,你会尝试做以下事情:
targets> target if="aaa" /> target if="bbb" /> /targets>
如果这是正确的,那么我认为你的解决方案不会这样做。我不确定我是否能理解你在代码中想要做什么。您设置组合框的方式在您使用作家时永远不会被检查。
这个怎么样:
代码:
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);
}
}
}
}
我希望这可以帮助您解决问题。