2017-01-25 47 views
0

你好我在使用wpf用戶控制時遇到了一些問題。我在用戶控制中有單個圖像。我想把這個用戶控件放在我的主窗口上,該窗口包含一些map.i將這個用戶控件(圖像)放在我的主窗口上。他們是10 t0 15。我想要的是通過單擊此用戶控件打開不同的窗口只是一個形象。但是當我這樣做的時候,每次都會向我展示同一個窗口。有沒有什麼辦法,所以我只需要創建一個用戶控制和多次使用它來打開不同的窗口。像(窗口1,窗口2,window3)等。usercontrol打開不同的窗口c#

好心幫我。我會非常感謝。

<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" /> 

在CS

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

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

將一些在問題 –

+0

您的相關代碼,我編輯我的問題。用戶控制 – ROMA

+0

請單擊此用戶控件,還包括當前打開窗口的部分。 –

回答

0

至於我可以通過給定的信息瞭解,要在不同的窗口中打開該用戶控件。我建議你創建一個窗口,並添加該用戶控件到它像

<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> 

然後在你的鼠標點擊事件:

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(); 
} 
+0

你沒明白我的意思。它會一直打開同一窗口,無論我使用多少次..我想通過單用戶控制打開不同的窗口 – ROMA

+0

使用阿里的方式,但檢查用戶點擊哪個圖釘並決定打開哪個窗口。只需使用上面相同的'clickit'方法,並添加一些'if()'語句來決定打開哪個窗口。 –

+1

@dubstylee我真的沒有得到創建這麼多窗口只是爲了顯示圖像的目的。我們可以將點擊的信息傳遞給我們的1個窗口並根據需要進行處理! –