Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

XML
<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!
Posted
Comments
Sergey Alexandrovich Kryukov 23-Jan-15 13:38pm    
I hope "same user control" does not mean same instance, it means "another user control of the same type". But what's the problem then?
One your problem is: this is incomplete WPF, won't build.
—SA

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
 
Share this answer
 
v2
Comments
Member 10894922 26-Jan-15 4:21am    
Thanks for the informative explenation. I think I was not very clear and therefore I try to update my question as follows.
Sergey Alexandrovich Kryukov 26-Jan-15 15:58pm    
Good. If you want to get my clarifications, don't forget to add a comment to this post when you are done.
—SA
UPDATE:
I do have following XAML code:

XML
<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??
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900