From what I understand so far, the InkCanvas element is in the WPF Framework. To use that, I need an ElementHost control to host the InkCanvas element. I've been to the MSDN links, but the example it gives talks of creating a WPF User Control Library project and so on. It's not that bad, but it seems a bit much to just add a control to a Winform. Is there a simpler way to do this, or am I trying to oversimplify this?
Thanks.
This should work:
ElementHost host = new ElementHost();
InkCanvas ic = new InkCanvas();
host.Child = ic;
Controls.Add(host);
As mentioned in comments, one needs to add the WPF assemblies as reference (WindowsBase, PresentationCore, PresentationFramework).
Related
I want to create a ToolStripDropDownButton which looks like the below image
But when I tried to search for ToolStripDropDownButton control in the Toolbox I was unable to find it because, after some googling, I found out that it is a class not namespace.
Then I googled out the code below
ToolStripDropDownButton dropDownButton1 = new ToolStripDropDownButton();
ToolStripDropDown dropDown = new ToolStripDropDown();
dropDownButton1.Text = "A";
dropDownButton1.DropDown = dropDown;
dropDownButton1.Height = 200;
dropDownButton1.Width = 200;
Set the drop-down direction.
dropDownButton1.DropDownDirection = ToolStripDropDownDirection.Left;
// Do not show a drop-down arrow.
dropDownButton1.ShowDropDownArrow = false;
Controls.Add((Control)dropDownButton1); //Doesn't work
But the last line of code is not valid and gives runtime error
Cannot convert type 'System.Windows.Forms.ToolStripDropDownButton' to
'System.Windows.Forms.Control'
Can someone tell me how to add such a button in C# Windows Form or what am I missing in the code?
Platform : VS2008 Express (i know it's old)
The error message says that ToolStripDropDownButton is not a Control and thus, you can't add it to your form directly.
ToolStripDropDownButton and other tool strip items only work as a part of a ToolStrip. So, you need to put a toolstrip on your form, then you can add items to it.
Code example, if you want to do it programmatically:
ToolStrip toolStrip = new System.Windows.Forms.ToolStrip();
toolStrip.Items.Add(dropDownButton1);
Controls.Add(toolStrip);
It looks like your code comes from ShowDropDownArrow example on MSDN. Check out the complete code.
Also, you can do it in a form designer in Visual Studio. Look for ToolStrip in a toolbox.
Relevant links:
MSDN: How to: Create a Basic Windows Forms ToolStrip with Standard Items
Using the Designer
MSDN: How to: Add ToolStrip Items Dynamically
C# Corner: ToolStrip in C#
StackOverflow: Windows.Forms button with drop-down menu - discussion of alternative ways to implement this kind of control. One of the answers suggests to use a standalone ToolStrip.
I'm writing a Windows Phone 8.1 Store app and I need to create a control so that it looks and behaves similar to WP 8.1 system task switcher (that appears when holding back hardware button).
It should show some images and support sliding left or right when swiping. Does anyone know what control should I use or do I need to create a completely new control from scratch?
So, the sollution was easy. The control I was looking for was... a ScrollViewer. It has 2 properties in WinRT XAML that make the ScrollViewer scrolling behave like I wanted: HorizontalSnapPointsAlignment and HorizontalSnapPointsType.
If you want to try this behavior, this MSDN code sample will expose it for you.
One point to mention. If you wish to set such behavior to, for example, a ListView you should firstly get its internal ScrollViewer in code and then set its HorizontalSnapPointsAlignment and HorizontalSnapPointsType properties. You can use GetFirstDescendantOfType<T>() extension method from WinRT XAML toolkit.
var sv = myListView.GetFirstDescendantOfType<ScrollViewer>();
sv.HorizontalSnapPointsAlignment = SnapPointsAlignment.Center;
sv.HorizontalSnapPointsType = SnapPointsType.Mandatory;
I've created a .DLL in WPF. To use it in existing Windows Forms application I use ElementHost.
ElementHost eleHOst = new ElementHost();
UserWarps userWarps = new UserWarps();
eleHOst.Child = userWarps;
eleHOst.Dock = DockStyle.Fill;
UserWarps is in WPF .DLL which has been add-referenced. Now the file does lot of 3D manipulations. I'm also using Petzold.Media3D for 3D lines for wireframe modelling. Everything's working fine except that WireLines of Petzold.Media3D is not drawing any lines. If I reference the DLL from other WPF applications everything's fine, but hosting the UserControl of wpf in windows forms eliminates the lines/wireframes. Rest everything is perfect - MeshGeometry3D, Models, Visuals, functionalities etc.
Please suggest the way forward. could any alternative to ElementHost work? If it does then what is it?
Adding answer originally added by OP in question as I don't want this question to
be closed just because of that.
Petzold has mentioned here that hosting wpf in Windows forms causes the Wire frames to disappear. He also posts a work around which is very simple and worked perfectly:
NOTE: For reasons discussed in paragraph 5, these Wire classes will
not work when
you're hosting 3D in Windows Forms, or when you're trying to print a 3D scene. To make it work, try replacing the static
OnRendering method in WireBase with the following:
static void OnRendering(object sender, EventArgs args)
{
foreach (WireBaseAndUltimateParent wirebaseAndParent in listWireBases)
{
WireBase wirebase = wirebaseAndParent.wirebase;
wirebase.OnRendering();
}
}
I have troubles animating a StackPanel with the VisualStateManager.
VisualStateManager.GoToState() accepts a Control as parameter, but StackPanel is of type UIElement/FrameworkElement.
How can I call VisualStateManager.GoToState() on a StackPanel?
You can use VisualStateManager.GoToElementState
This post might help you: WPF using VisualStateManager to animate panels in & out.
Edit:
The methods above are only for WPF and not available in Silverlight. For Silverlight the ExtendedVisualStateManager from the Blend SDK might be helpful:
http://msdn.microsoft.com/de-de/library/microsoft.expression.interactivity.core.extendedvisualstatemanager.gotoelementstate(v=expression.40).aspx
You would have to include the Microsoft.Expression.Interactions assembly though.
Can we cast a WPF User Control to a form control??
I'm sorry you can't. WPF works very differently internally from Winforms: Winforms uses the controls provided by the Windows OS (where each control has a window handle), where WPF uses DirectX to do the painting.
You can host WPF controls inside winforms applications (EDIT)and vice versa (with limitations) but that is perhaps not what you're after.
I tried this out:
TouchScreenWPF touchUI = new TouchScreenWPF();
ElementHost elementHost = new ElementHost();
elementHost.Child = touchUI;
Control userControl = new Control();
userControl.Controls.Add(elementHost);
The form contains the usercontrol, but does not display anything when I include a WPF User control. It works with a single button though... Am I missing something there?