[英]Adding WPF controls on WindowsFormsHost?
如何在WindowsFormsHost上添加WPF控件(我已经知道,当我将Child添加到WFH时,它将成为一个单独的句柄)。 我尝试将其单独放置为StackPanel或Canvas,但似乎无法正常工作:
class CustomCanvas : Canvas{
public CustomCanvas(/*Some Width, Height, path values received*/){
WindowsFormsHost _AnimatedBckgr = new WindowsFormsHost()
{
Width = _wdt,
Height = Container_Height,
Margin = new Thickness(0,0,0,0),
Child = new System.Windows.Forms.Panel()
};
((System.Windows.Forms.Panel)_AnimatedBckgr.Child).Controls.Add(new System.Windows.Forms.PictureBox()
{
//Width = (int)_wdt, Height = (int)Container_Height,
Dock = System.Windows.Forms.DockStyle.Fill,
BackgroundImage = new System.Drawing.Bitmap(GifAnimatedFilePath),
BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch,
});
//StackPanel or Canvas wndHost;
wndHost = new StackPanel() { Width = _wdt, Height = Container_Height };
wndHost.Children.Add(_AnimatedBckgr);
Children.Add(wndHost);
//Anything added in this canvas doesn't appears over the wndHost
Children.Add(new Button(){ Content = "Hi" });
}
}
是否有其他方法可以在WindowsFormsHost上添加WPF控件,而无需使用骇人的透明Windows hack? 谢谢。
在C#程序代码中做了一些编写之后,我努力在主机上添加一个Host,以此类推。因此,我可以在覆盖WPF控件的WinForms控件上添加WPF控件...有些奇怪的东西,但是您可以自己尝试,如果在添加时未定义ElementHost
或WindowsFormsHost
大小,请注意.NET中存在sme错误。 例:
/*In a WPF Window, content container being a Canvas, adding a new WFH with
a `PicturBox()` on it. Then add it a DataGridView OVER this one, and then,
some `Label` over it*/
public partial class MainWindow : Window
{
public MainWindow()
{
Content = new Canvas();
System.Windows.Forms.Integration.WindowsFormsHost _wndHost = new System.Windows.Forms.Integration.WindowsFormsHost()
{
Margin = new Thickness(0, 0, 0, 0),
Width = 200,
Height = 200,
Child = new System.Windows.Forms.PictureBox()
{
BackgroundImage = new System.Drawing.Bitmap(System.IO.Directory.GetCurrentDirectory() + "\\f.jpg"),
BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch
}
};
((System.Windows.Forms.PictureBox)_wndHost.Child).Controls.Add(new System.Windows.Forms.DataGridView()
{
Width = 170, Height = 70,
ColumnCount = 2, RowCount = 2
});
((System.Windows.Forms.PictureBox)_wndHost.Child).Controls.Add(new System.Windows.Forms.Integration.ElementHost()
{
Width = 70, Height = 15,
Child = new TextBlock()
{
Width = 10,
Height = 10,
Text = "First :O",
Foreground = Brushes.Red,
Background = Brushes.Transparent
},
BackColor = System.Drawing.Color.Transparent,
Location = new System.Drawing.Point(10, 100)
});
((System.Windows.Forms.PictureBox)_wndHost.Child).Controls.Add(new System.Windows.Forms.Integration.ElementHost()
{
Width = 70, Height = 15,
Child = new TextBlock()
{
Width = 10,
Height = 10,
Text = "Second :O",
Foreground = Brushes.Red,
Background = Brushes.Transparent
},
BackColor = System.Drawing.Color.Transparent,
Location = new System.Drawing.Point(10, 150)
});
((Canvas)Content).Children.Add(_wndHost);
}
}
好的,这更加接近了……将Windows窗体用户控件添加到您的项目中:
在该用户控件中添加您的图片:
现在回到XAML并添加WFH ...,添加刚刚创建的usercontrol的子级。
<Canvas>
<WindowsForsHost HorizontalAlignment="Left" Height="34" VerticalAlignment="Top" Width="52">
<local:UserControl1/>
</WindowsformsHost>
<Label Margin="0,35,-13,0">This is bill Clinton</Label>
</Canvas>
请注意,在标记中,标签的偏移量可以放置在画布上您想要的任何位置。
结果是这样,但它没有动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.