如何在 tabitem 中动态添加复选框?
我有一个按钮单击事件,这应该向我的用户界面添加一个复选框。正确的做法是什么?
我尝试过这个:
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);
}
}
}
但它似乎不起作用。当我输入 Tabitem5.Controls 时,智能感知没有给我“控件”选项。
我的xaml结构是这样的:
<Window ...>
<TabControl..>
<TabItem Name="Tabitem1">
<Grid>
</Grid>
</TabItem>
<TabItem Name="Tabitem5">
<Grid>
</Grid>
</TabItem>
</TabControl>
</Window>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,控件不是使用 WPF 直接从代码后面添加的。完成您建议使用 MVVM 之类的任务的通常方法是将按钮按下连接到 ViewModel 上的命令,然后连接到 XML 中的 ViewModel 加载。 XML 中的项目将进入一个集合,可能是一个 ObservableCollection。然后,该集合将绑定到类似 ItemsControl 之类的东西,该控件具有涉及复选框的模板。您可以在 http://msdn.microsoft.com/en- 找到有关 MVVM 的更多信息us/magazine/dd419663.aspx
无论如何,您都在尝试将 Windows 窗体 CheckBox 控件与 WPF 混合使用。除非您在 WPF 内部进行特殊的 WinForms 托管,否则这是行不通的。您将需要 System.Windows.Controls.CheckBox。另外,您似乎正在尝试直接添加到 TabItem.Controls。 WPF TabItem 没有 Controls 属性 - 您确定使用 WPF 控件吗?
出于好奇,您是否要将 WinForms 应用程序移植到 WPF?
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?