选择Treeviewitem并打开新窗口(WPF)
大家好,
我是WPF的新手,
我将TreeViewControl
作为菜单项放在DockPanel
的左侧,我从数据库中填充了菜单项,并用TreeViewItem
填充了菜单项,这可以正常工作.
现在,我想打开一个相应的表单(WPF Windows表单),该表单将在与该TreeViewItem
链接的DockPanel
(中心)中打开.
在我的Selected_Changed
的RoutedEventArgs
中,下面的代码如下:
Hello All,
I am newbie to WPF,
I have TreeViewControl
as menu item in DockPanel
left hand side and I populate the menu items from my database and fill it up with TreeViewItem
''s, which works correctly.
Now I would like to open a corresponding form (WPF Windows form) to be opened in DockPanel
(Center) which is linked with that TreeViewItem
.
In my RoutedEventArgs
of Selected_Changed
below is below code:
TreeViewItem selectedItem = (TreeViewItem)MenuTree.SelectedItem;
if (selectedItem.Tag.ToString() != null)
{
DataRow[] Dr = MnuDs.Tables[0].Select("MenuId=" + selectedItem.Tag.ToString());
if (Dr.Length > 0)
{
if (Dr[0]["FormName"].ToString() != "")
{
Window frm = (Window)this.GetType().Assembly.CreateInstance("myprojectName." + Dr[0]["FormName"].ToString());
if (frm != null)
{
frm.Show();
frm.Focus();
}
}
Dr = null;
}
}
我想对上面的代码问几个问题.
1)这是在WPF中执行的正确方法吗?如果不是,那么想寻找适当的代码.
2)我的对应形式打开时任何
提前非常感谢您.
I would like to ask few question on above code.
1) Is this the right way to do it in WPF, if not then, would like to seek a proper code.
2) My corresponding form opens when any TreeViewItem
is selected , but doesn''t open in DockPanel
(center) (rather I do not know where to write the code to open in center) and opens in separate window.
Thank you very much in advance.
嗨.您可以将WindowsFormsHost放置在扩展面板中.当用户选择树形视图项目时,通过反射找到所需的元素(如上所示),然后将其放置在WindowsFormsHost中.我知道这可以在Windows窗体控件中使用,但是我不确定它是否可以在窗口中使用.
Hi. You could place a WindowsFormsHost inside your dockpanel.When the user selects the tree view item, find a desired element via reflection(like you demonstrated above) and place it inside WindowsFormsHost. I know this works with windows form controls but I am not sure that it will work with the window.