i have a button click event this supposed to add a checkbox to my UI. what is the correct way of doing it?
i tried this:
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");
foreach(string target in abc)
{
if (!Dictionarycheck.ContainsKey(target))
{
System.Windows.Forms.CheckBox chk = new System.Windows.Forms.CheckBox();
chk.Text = target;
Tabitem5.Controls.Add(chk);
}
}
}
but it doesn't seem to work. When i enter Tabitem5.Controls, the intellisense does not give me the option of Controls.
my xaml structure is like this:
<Window ...>
<TabControl..>
<TabItem Name="Tabitem1">
<Grid>
</Grid>
</TabItem>
<TabItem Name="Tabitem5">
<Grid>
开发者_开发问答 </Grid>
</TabItem>
</TabControl>
</Window>
Typically controls are not added directly from code behind using WPF. The usual way to accomplish something like you're proposing using MVVM would be to wire up the button press to a Command on the ViewModel and then on the ViewModel load in your XML. The items from the XML would go into a collection, probably an ObservableCollection. The collection would then be bound to something like an ItemsControl that has a template involving a checkbox. You can find more on MVVM at http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
In any case, you're trying to mix a Windows Forms CheckBox control with WPF. Unless you do special WinForms hosting inside of WPF, that's not going to work. You'll want System.Windows.Controls.CheckBox instead. Also, it looks like you're trying to add directly to a TabItem.Controls. The WPF TabItem does not have a Controls property - are you sure you're using the WPF control for that?
Just out of curiosity, are you porting a WinForms app to WPF?
精彩评论