如何在WPF中多次加载相同的UserControl? [英] How do I load the same UserControl in WPF several times?

查看:976
本文介绍了如何在WPF中多次加载相同的UserControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 winform UserControl ,我需要在我的 WPF - 应用程序中使用它。到目前为止,我可以在WPF中加载和使用此UserControl。 XAML代码是:



 <  窗口    x:Class   =  P13_LL_DV_Bench.MainWindow  

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x = http://schemas.microsoft.com/winfx/2006 / xaml

< span class =code-attribute> xmlns:wf = clr- namespace:LowLightGrab; assembly = LowLightGrab


< span class =code-attribute>


< WindowsFormsHost 高度 = 947 名称 = image_LowLight 宽度 = 1430 > ;
< wf:UserControl1 / >
< / WindowsFormsHost >







现在我需要使用相同的UserControl再次在我的WPF应用程序中。

问题:如何更改XAML代码?



谢谢!

解决方案

请看我对这个问题的评论。



如果你的用户控件基本上有效,我猜你的问题不是用户控件或Forms互操作,而是一般的XML设计和布局。您不能使用相同的用户控件,如果需要,可以使用同一类型的另一个用户控件。问题在于你把它放在哪里。



首先,你需要一个容器来放置更多控件。在最简单的情况下,窗口的直接逻辑应该是一些 panel ,即从类 System.Windows.Controls.Panel ,例如 System.Windows.Controls.Grid

https://msdn.microsoft.com/en-us/library/system.windows.controls.panel %28v = vs.110%29.aspx#inheritanceContinued [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.controls.grid%28v = vs.110%29.aspx [ ^ ](例如)。



面板允许您将多个UI元素作为子元素。选择您想要使用的内容并阅读使用情况。



只需在此类面板的另一个单元格或不同的面板中添加类似的XAML声明。 / b>



基本的一般理解:一些WPF元素可以通过<$ c $在逻辑树中有一个子元素c>内容
属性,其他可以有几个孩子。有关详细信息,请阅读并理解:

https://msdn.microsoft.com/en-us/library/vstudio/bb613556%28v=vs.90%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/ ms753391%28v = vs.110%29.aspx [ ^ ]。



另一个问题是尺寸和布局。基本上,您可以选择窗口的大小,然后根据布局占用可用空间,这可能非常复杂。例如, Grid 可以将某些列宽定义为自动,具体取决于内容的大小,其他可以按您定义的比例划分剩余的房间。不同的UI元素具有不同的布局特征。或者,您可以保留窗口的默认大小(NaN,完全跳过XAML中的属性),然后内容的大小将定义窗口的大小。如果您定义用户控件和父级(比如窗口)大小的大小,则可能会发生冲突。设计布局以自动定义尺寸。对于窗口内容定义的大小,请参阅:

https://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.sizetocontent%28v=vs.110%29.aspx [ ^ ]。



请注意,此决定是针对垂直和水平布局单独完成的。



参见:

< a href =https://msdn.microsoft.com/en-us/library/ms745058%28v=vs.110%29.aspx> https:/ /msdn.microsoft.com/en-us/library/ms745058%28v=vs.110%29.aspx [ ^ ],

http://wpftutorial.net/LayoutProperties.html [ ^ ]。



-SA


更新:

我有以下XAML代码:



 <   window     x: class   =  P13_LL_DV_Bench.MainWindow    xmlns:x   = #unknown >  
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http:// schemas .microsoft.com / winfx / 2006 / xaml
xmlns:wf =clr-namespace:LowLightGrab; assembly = LowLightGrab
xmlns:wf_zoom0 =clr-namespace:LowLightGrab; assembly = LowLightGrab
xmlns:wf_zoom1 =clr-namespace:LowLightGrab; assembly = LowLightGrab
xmlns:wf_zoom2 =clr-namespace:LowLightGrab; assembly = LowLightGrab
xmlns:wf_zoom3 =clr-namespace: LowLightGrab; assembly = LowLightGrab

< canvas panel.zindex = 1 margin = 0,0,0,0 height = 947 >
< grid horizo​​ntalalignment = 拉伸 verticalalignment = 拉伸 名称 = grid2 height = 947 width = 1430 canvas.top = 0 canvas.left = 0 >
< grid.rowdefinitions >
< < span class =code-leadattribute> rowdefinition
height = 10 * / >
< rowdefinition 高度 = 463 * / >
< rowdefinition height = * / >
< < span class =code-leadattribute> rowdefinition height = 463 * / > ;
< rowdefinition height = 10 * / >
< / grid.rowdefinitions >
< grid.columndefinitions >
< columndefinition < /跨度> <温泉n class =code-attribute> width = 0 * / >
< columndefinition width = 10 * / >
< columndefinition width = 705 * / >
< columndefinition width = * / >
< columndefinition width = 704 * / >
< < span class =code-leadattribute> columndefinition width = 10 * / >
< / grid.columndefinitions >
< image horizo​​ntalalignment = 拉伸 name = image_fokus stretch = verticalalignment =
拉伸 grid.column = 1 grid.columnspan = 5 grid.rowspan = 5 / < span class =code-keyword>>
< image grid.column = 2 grid.colum nspan = 3 grid.row = 1 grid.rowspan = 3 height = 467 horizo​​ntalalignment = < span class =code-attribute> margin = 318,235,0,0 名称 = image_test stretch = 填写 verticalalignment = 顶部 width = 667 / >

< windowsformshost grid.column = 1 grid.columnspan = 5 grid.rowspan = 6 height = 948 horizo​​ntalalignment = 名称 = image_LowLight verticalalignment = 顶部 width = 1430 >
< wf:usercontrol1 xmlns:wf = #unknown / >
< / windowsformshost >

< windowsformshost grid.column = 1 grid.columnspan = 3 grid.rowspan = 2 height = 473 horizo​​ntalalignment = margin = 1,0,0,0 name = wf_zoom0 verticalalignment = Top width = 715 >
< wf_zoom0:usercontrol1 xmlns:wf_zoom0 = #unknown / >
< / windowsformshost >

< windowsformshost grid.column = 1 grid.columnspan = 3 grid.row = 2 grid.rowspan = 3 height = < span class =code-keyword> 473 horizo​​ntalalignment = 保证金 = 1,0,0,0 name = wf_zoom1 verticalalignment = < span class =code-keyword> Top width = 715 >
< wf_zoom1:usercontrol1 xmlns:wf_zoom1 = #unknown / >
< / window sformshost >

< windowsformshost grid.column = 3 grid.columnspan = 3 grid.rowspan = 3 < span class =code-attribute> height = 473 horizo​​ntalalignment = margin = 0, 1,0,0 名称 = wf_zoom2 verticalalignment = Top width = 715 >
< wf_zoom2:usercontrol1 xmlns:wf_zoom2 = #unknown / >
< / windowsformshost >

< windowsformshost grid.column = 4 grid .columnspan = 2 grid.row = 2 grid.rowspan = 3 height = 473 horizo​​ntalalignment = 名称 = wf_zoom3 verticalalignment = Top 宽度 = 715 > ;
< wf_zoom3:usercontrol1 xmlns:wf_zoom3 = #unknown < span class =code-keyword> / >
< / windowsformshost >


< / grid >
</canvas>
</window>





What you see here is that I am using five times the same UserControl \"LowLightGrab\" at the beginning and in a WindowsFormsHost below.

The problem is that I only see the U serControl Name=\"image_LowLight\" but not the other UserControls when I set them visible. I recognized that this UserControl is shown, which is first set in the WindowsFormsHost. In this case the \"LowLightGrab\" is shown while the others are \"ignored\".



Is it not possible to use the same UserControl in the way I want to??


I do have a winform UserControl which I need to use in my WPF-application. So far I can load and use this UserControl in WPF. The XAML-code is:

<Window x:Class="P13_LL_DV_Bench.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:wf="clr-namespace:LowLightGrab;assembly=LowLightGrab" 





        <WindowsFormsHost Height="947" Name="image_LowLight" Width="1430" >
              <wf:UserControl1/>
        </WindowsFormsHost>




Now I need to use the same UserControl again in my WPF-application.
Question: How do I need to change the XAML code?

Thanks!

解决方案

Please see my comment to the question.

If your user controls basically works, I guess your problem is not user control or Forms interop, but general XML design and layout. You cannot use "the same user control", you can use "another user control of the same type", if you need to. The problem is where you put it.

First of all, you need a container to put more controls. In the simplest case, the immediate logical of you window should be some panel, that is, a UI element derived from the class System.Windows.Controls.Panel, such as System.Windows.Controls.Grid:
https://msdn.microsoft.com/en-us/library/system.windows.controls.panel%28v=vs.110%29.aspx#inheritanceContinued[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.grid%28v=vs.110%29.aspx[^] (for example).

Panels allow you to have multiple UI element as children. Choose what you want to use and read on the usage.

And simply add the similar XAML declaration in another cell of such panel or a different panel.

For basic general understanding: some WPF elements can have one child element in a logical tree, via the Content property, and other can has several children. For further detail, please read and understand it:
https://msdn.microsoft.com/en-us/library/vstudio/bb613556%28v=vs.90%29.aspx[^],
https://msdn.microsoft.com/en-us/library/ms753391%28v=vs.110%29.aspx[^].

Another your problem is sizes and layout. Basically, you either choose the size of the window, and then all your content should occupy available space according to layout, which can be pretty sophisticated. For example Grid can define some column width as automatic, depending on the size of content, and other could divide the remaining rooms in proportions you define. Different UI element have different layout features like that. Alternatively, you can leave default size of the window (NaN, skip the attributes in XAML at all), and then the size of content will define the size of the window. If you define both size of, say, your user control and parent (say, window) size, it may conflict. Design your layout to automatically define sizes. For the size defined by window's contents, please see:
https://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.sizetocontent%28v=vs.110%29.aspx[^].

Note that this decision is done separately for vertical and horizontal layout.

See also:
https://msdn.microsoft.com/en-us/library/ms745058%28v=vs.110%29.aspx[^],
http://wpftutorial.net/LayoutProperties.html[^].

—SA


UPDATE:
I do have following XAML code:

<window x:class="P13_LL_DV_Bench.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:LowLightGrab;assembly=LowLightGrab" 
        xmlns:wf_zoom0="clr-namespace:LowLightGrab;assembly=LowLightGrab" 
        xmlns:wf_zoom1="clr-namespace:LowLightGrab;assembly=LowLightGrab" 
        xmlns:wf_zoom2="clr-namespace:LowLightGrab;assembly=LowLightGrab" 
        xmlns:wf_zoom3="clr-namespace:LowLightGrab;assembly=LowLightGrab" 
		
            <canvas panel.zindex="1" margin="0,0,0,0" height="947">
                <grid horizontalalignment="Stretch" verticalalignment="Stretch" name="grid2" height="947" width="1430" canvas.top="0" canvas.left="0">
                    <grid.rowdefinitions>
                        <rowdefinition height="10*" />
                        <rowdefinition height="463*" />
                        <rowdefinition height="*" />
                        <rowdefinition height="463*" />
                        <rowdefinition height="10*" />
                    </grid.rowdefinitions>
                    <grid.columndefinitions>
                        <columndefinition width="0*" />
                        <columndefinition width="10*" />
                        <columndefinition width="705*" />
                        <columndefinition width="*" />
                        <columndefinition width="704*" />
                        <columndefinition width="10*" />
                    </grid.columndefinitions>
                    <image horizontalalignment="Stretch" name="image_fokus" stretch="None" verticalalignment="Stretch" grid.column="1" grid.columnspan="5" grid.rowspan="5" />
                    <image grid.column="2" grid.columnspan="3" grid.row="1" grid.rowspan="3" height="467" horizontalalignment="Left" margin="318,235,0,0" name="image_test" stretch="Fill" verticalalignment="Top" width="667" />

                    <windowsformshost grid.column="1" grid.columnspan="5" grid.rowspan="6" height="948" horizontalalignment="Left" name="image_LowLight" verticalalignment="Top" width="1430">
                        <wf:usercontrol1 xmlns:wf="#unknown" />
                    </windowsformshost>

                    <windowsformshost grid.column="1" grid.columnspan="3" grid.rowspan="2" height="473" horizontalalignment="Left" margin="1,0,0,0" name="wf_zoom0" verticalalignment="Top" width="715">
                        <wf_zoom0:usercontrol1 xmlns:wf_zoom0="#unknown" />
                    </windowsformshost>

                    <windowsformshost grid.column="1" grid.columnspan="3" grid.row="2" grid.rowspan="3" height="473" horizontalalignment="Left" margin="1,0,0,0" name="wf_zoom1" verticalalignment="Top" width="715">
                        <wf_zoom1:usercontrol1 xmlns:wf_zoom1="#unknown" />
                    </windowsformshost>

                    <windowsformshost grid.column="3" grid.columnspan="3" grid.rowspan="3" height="473" horizontalalignment="Left" margin="0,1,0,0" name="wf_zoom2" verticalalignment="Top" width="715">
                        <wf_zoom2:usercontrol1 xmlns:wf_zoom2="#unknown" />
                    </windowsformshost>

                    <windowsformshost grid.column="4" grid.columnspan="2" grid.row="2" grid.rowspan="3" height="473" horizontalalignment="Left" name="wf_zoom3" verticalalignment="Top" width="715">
                        <wf_zoom3:usercontrol1 xmlns:wf_zoom3="#unknown" />
                    </windowsformshost>


                </grid>
            </canvas>
</window>




What you see here is that I am using five times the same UserControl "LowLightGrab" at the beginning and in a WindowsFormsHost below.
The problem is that I only see the UserControl Name="image_LowLight" but not the other UserControls when I set them visible. I recognized that this UserControl is shown, which is first set in the WindowsFormsHost. In this case the "LowLightGrab" is shown while the others are "ignored".

Is it not possible to use the same UserControl in the way I want to??


这篇关于如何在WPF中多次加载相同的UserControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆