WindowsFormHost油漆事件未触发



我在WPF应用程序中有一个WindowsFormHost面板。最终我需要能够单击并绘制点。我让它完全工作在一个单独的WinForm形式…但是由于某些原因,当Invalidate()被调用时,paint事件根本不会触发!点击事件肯定会在点击时出现消息框时触发。但油漆里一点动静都没有。我怎么也想不明白为什么……下面是代码。

XML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    Title="MainWindow" Height="1000" Width="1000"
    WindowState="Maximized" SizeChanged="Window_SizeChanged" LocationChanged="Window_LocationChanged" Background="Gray">
<DockPanel LastChildFill="True">
    <ToolBarTray DockPanel.Dock="Top">
    <ToolBar Width="Auto" HorizontalAlignment="Stretch">
            <Menu>
                <MenuItem Header="File" VerticalAlignment="Center">
                        <MenuItem Header="New" Click="MenuItem_Click_3" />
                        <MenuItem Header="Open" Click="MenuItem_Click_4"/>
                        <MenuItem Header="Open Recent">
                            <MenuItem Header="Some Recent Files"/>
                        </MenuItem>
                        <MenuItem Header="Import H2OFate File" Click="MenuItem_Click_7"/>
                        <Separator/>
                        <MenuItem Header="Save" Click="MenuItem_Click_1" />
                        <MenuItem Header="Save As" Click="MenuItem_Click_5"/>
                        <MenuItem Header="Save without Results"/>
                        <MenuItem Header="Delete Unnecessary Files"/>
                        <Separator/>
                        <MenuItem Header="File Options"/>
                        <Separator/>
                        <MenuItem Header="Project Info"/>
                        <MenuItem Header="Supply Info" />
                        <Separator/>
                        <MenuItem Header="Print" Click="MenuItem_Click_6" />
                        <Separator/>
                        <MenuItem Header="Reload Current File"/>
                        <MenuItem Header="Exit                CTRL+Q" Click="MenuItem_Click"/>
                        </MenuItem>
            <MenuItem Header="Network Design" VerticalAlignment="Center">
                        <MenuItem Header="Something" Click="MenuItem_Click_8"/>
            </MenuItem>
            <MenuItem Header="Run Analysis" VerticalAlignment="Center">
                    <MenuItem Header="Run" Click="MenuItem_Click_2"/>
            </MenuItem>
            <MenuItem Header="View Results" VerticalAlignment="Center">
                    <MenuItem Header="Numeric"/>
                    <MenuItem Header="X-Y Plots"/>
                    <MenuItem Header="Contours"/>
                    <MenuItem Header="Reports"/>                                                
            </MenuItem>
            <MenuItem Header="Facilities Management" VerticalAlignment="Center">
                    <MenuItem Header="Some Item"/>
                    <MenuItem Header="Hydraulics"/>
                    <MenuItem Header="Contamination"/>
                    <MenuItem Header="Sensor Placement"/>
            </MenuItem>
            <MenuItem Header="Tools/Databases" VerticalAlignment="Center">
                    <MenuItem Header="Some Item"/>
                    <MenuItem Header="Hydraulics"/>
                    <MenuItem Header="Contamination"/>
                    <MenuItem Header="Sensor Placement"/>
            </MenuItem>
            <MenuItem Header="Help" VerticalAlignment="Center">
                    <MenuItem Header="Some Item"/>
                    <MenuItem Header="Help"/>
                    <MenuItem Header="Units"/>
                    <MenuItem Header="Demo Examples"/>
                    <MenuItem Header="Tutorial"/>
                    <MenuItem Header="How To"/>
                    <MenuItem Header="About"/>
            </MenuItem>
            </Menu>
            </ToolBar>
    <ToolBar>
            <Button Height="38" Width ="50" VerticalAlignment="Center" Click="Button_Click_1">                        
                <Ellipse Width="25" Height="25" Fill="Blue" Stroke="Black" VerticalAlignment="Center"/>
            </Button >
            <Button Height="38" Width ="50" VerticalAlignment="Center" Click="Button_Click_1">
                <Rectangle Width="38" Fill="Red" Height="7" Stroke="Black"/>
            </Button >
            <Button Height="38" Width ="50" VerticalAlignment="Center" Click="Button_Click_2">
                <Polygon Fill="Yellow" Stroke="Black" Points="0,10 30,10 30,0 28,0 28,3 2,3 2,0 0,0" VerticalAlignment="Center"/>
            </Button >
            <Button Height="38" Width ="50" VerticalAlignment="Center" Click="Button_Click_3">
                <Polygon Stroke="Black" Fill="Green" VerticalAlignment="Center" Points="22,0 22,7 15,7 15,15 9,15 9,7 0,7 0,0  "/>
            </Button > 
    </ToolBar>
        </ToolBarTray>
            <TabControl Width ="1000" DockPanel.Dock="Left">
        <TabItem Header="Map">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <WindowsFormsHost Grid.Row="0" x:Name="wfPanel" >
                    <WindowsFormsHost.Child>
                        <wf:Panel x:Name="wfSurface" MouseClick="wfSurface_MouseClick"/>
                    </WindowsFormsHost.Child>
                </WindowsFormsHost>
            </Grid>
        </TabItem>
        <TabItem Header="Map Settings">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="250"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Button Content="Testing 123"/>
            </Grid>
        </TabItem>
    </TabControl>
    <TabControl DockPanel.Dock="Right">
    </TabControl>
</DockPanel>   

c#

private void wfSurface_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        Graphics g;
        g = wfSurface.CreateGraphics();
        epanet epa = epanet.GetInstance();
        SolidBrush s = new SolidBrush(System.Drawing.Color.Blue);
        g.FillEllipse(s, Convert.ToInt32(epa.xCord), Convert.ToInt32(epa.yCord), 50, 50);
        g.FillEllipse(s, 400, 400, 500, 500);
        MessageBox.Show("paint event fired");
    }
    private void wfSurface_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        epanet epa = epanet.GetInstance();
        epa.xCord = e.X;
        epa.yCord = e.Y;
        MessageBox.Show("xCord is:  " + e.X.ToString());
        wfSurface.Invalidate();
    }
编辑:

为了进一步检查绘制事件是否存在问题,我将g.FillEllipse(s,400,400,400,400);线添加到click事件中,并成功绘制了椭圆

不是一个WPF的家伙,但我的猜测是添加这个:

<WindowsFormsHost.Child>
  <wf:Panel x:Name="wfSurface" Paint="wfSurface_Paint"
                               MouseClick="wfSurface_MouseClick"/>
</WindowsFormsHost.Child>

相关内容

  • 没有找到相关文章

最新更新