【问题标题】:usercontrol open different windows c#用户控件打开不同的窗口c#
【发布时间】:2017-01-25 22:23:56
【问题描述】:

您好,我在 wpf 中使用用户控件时遇到了一些问题。我在用户控件中有单个图像。我想将此用户控件放在包含一些地图的主窗口上。我正在将此用户控件(图像)放在我的主窗口上。它们是 10 t0 15。我想要通过单击此用户控件打开不同的窗口只是一个图像。但是当我这样做时,它每次只显示相同的窗口。有什么办法,所以我只需要创建单个用户控件并多次使用它来打开不同的窗口。比如(window1,window2,window3)等。

请帮助我。我会很感激的。

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

【问题讨论】:

  • 将您的一些相关代码放在问题中
  • 我编辑了我的问题。那是用户控件
  • 还请包括您当前通过单击此用户控件打开窗口的部分。
  • 看这里它的打开主...但是我如何通过放置单个用户控件来打开不同的窗口..
  • 打开这些不同窗口的标准是什么?我相信您只想根据单击的图像在窗口中打开不同的图像,不是吗?

标签: c# wpf


【解决方案1】:

据我所知,您希望在不同的窗口中打开此 UserControl。我建议你创建一个窗口并将这个 UserControl 添加到它上面,就像

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

【讨论】:

  • 你没有明白我的意思..无论我使用多少次它都会打开同一个窗口..我想通过使用单个用户控件打开不同的窗口
  • 使用阿里的方式,但是检查用户点击了哪个图钉,然后决定打开哪个窗口。只需使用上面相同的clickit 方法,并添加几个if() 语句来决定打开哪个窗口。
  • @dubstylee 老实说,我没有得到创建这么多窗口来显示图像的目的。我们可以将点击的信息传递给我们的1个窗口,根据需要进行处理!
  • @AliBaig 我同意,但是 OP 想要有单独的窗口,所以我只是建议他如何做到这一点:)
  • 它不像我创建这么多窗口来显示图像..这个图像将像按钮一样工作。我想把这个用户控件的图像放在有地图的主窗口上。所以每当点击这个用户控件时,我想显示窗口..例如,如果在不同的位置放置了 5 个,它应该显示 5 个不同的窗口......只有一个用户控件。
猜你喜欢
  • 1970-01-01
  • 2011-01-12
  • 2014-09-20
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
  • 2010-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多