简体   繁体   中英

usercontrol open different windows c#

hello i am having some problem while using user control in wpf. i have single image in user control. i want to place this user control on my main window which contains some map.i am placing this user control(image) on my main window.they are 10 t0 15.what i want is to open different windows by clicking on this usercontrol which is just an image. but when i do that it show me only same window every time. is there any way so i just have to create single user control and use it multiple times to open different windows.like (window1,window2,window3) etc..

kindly help me. i will be really thankfull.

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Demo.WindowsPresentation.CustomMarkers.CustomMarkerDemo"
Height="40" Width="30" Opacity="10">
    <Image MouseDown="clickit" Name="icon" Source="bigMarkerGreen.png" VerticalAlignment="Center" HorizontalAlignment="Center" />

In cs

this.MouseDown += new MouseButtonEventHandler(clickit);
     void clickit(object sender, MouseButtonEventArgs e)
        {

            MessageBox.Show("test");
            main.show();
        }

As far as I could understand by the given information, you want to open this UserControl in different windows. I would suggest you to create a window and add this UserControl onto it like

<Window x:Class="Demo.WindowsPresentation.CustomMarkers.win.winCustomMarker"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:marker="clr-namespace:Demo.WindowsPresentation.CustomMarkers"
        Title="Window" Height="345" Width="380">
    <Grid>
        <marker:CustomMarkerDemo></marker:CustomMarkerDemo>
    </Grid>
</Window>

And then in your mouse click event:

void clickit(object sender, MouseButtonEventArgs e)
{

    winCustomMarker customMarkerWindow = new winCustomMarker();
    //bind any information you want that you want to pass to your inner UserControl
    customMarkerWindow.show();
}

You may consider launching them as separate processes. Move user control to a new WPF project(.exe) and from main app create as many instances of user control you need.

        var imgUserControlProcess = new ProcessStartInfo
        {
            FileName = "ImageUserControl.exe",
            UseShellExecute = false,
            CreateNoWindow = false,
            WindowStyle = ProcessWindowStyle.Normal
        };

        var processStart = new Process
        {
            StartInfo = mainViewProcess
        };
        processStart.Start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM