I'm writing a program to implement COM component in C#. This component provides a UI component too. In the interest of making it easy to implement future additions to the component, I decided to write the UI in Xaml using WPF and wrapping it in an ElementHost control to provide the handle back to to the COM consumer.
Here's where this gets interesting. The element is being hosted in a win32 tab control. The first time the tab is selected, it loads fine. Switching to another tab and coming back causes it to not draw the component correctly. If I move the mouse through it, it'll draw the TextBox elements, but it'll never redraw the whole UserComponent.
I've tried every possible solution I've seen suggested, from changing the height/width values of the component to putting the HwndTarget in SoftwareOnly rendering mode.
I'm not sure what to try at this point, so suggestions are welcome. I'd really like to do this without using Windows Forms as the future maintainers of this aren't the best programmers and it's a lot easier to tell them to write up a Xaml file with data bindings than manually lay out a form and set the values in the code-behind.
Haven't met such problem yet.
Try to force refresh or focus (methods) to the hosting wrapper or HwndSource when switching to the hosting tab.
Related
I am learning multi threading in Win Forms using C# and according to sources the best way to achieve this is by invoking the main method from worker threads.
Now this all works good when heavy processing must be done and THEN the GUI is updated.
However I have a scenario where I need to programmatically add lots of controls inside a panel. This may go up to thousands (panel will be scrollable). Hence, since the controls are ultimately being added by the main thread, the program still hangs until this has been completed.
Is there any way around this? Or should I try and use some other control which doesn't require me to instantiate lots of controls simultaneously (as this is obviously a bit heavy).
Basically this panel contains a list together with an icon (depending on the state). Hence I am creating a label for every entry which I do not know if it the ideal way.
By the way I come from the web applications development department... Is there a control similar to a div in .NET? I looked at a rich text box but it doesn't seem to let you add an image in a straightforward way.
Thanks in advance.
You shouldn't have a good reason to add hundreds of controls, let alone thousands. It sounds like you need a custom control and you need to add items to it.
The ListBox or ListView control will work for a basic item but if you want lots of customizability you will have to reinvent the wheel yourself and draw everything manually. It's a lot of work if you need to handle multiselect, scrolling, keyboard shortcuts, etc.
This is the strength of using WPF instead of Winforms since you can easily use the existing ListBox logic and have free range to customize the appearance of the items and even how they are arranged.
WPF has the concept of a virtualizing panel which can perform well even with thousands of items since it doesn't create the UI objects until an item is scrolled to.
This is a broad question, I am aware. However, I have been trying to make a modification in a C# source code to enable a ToolboxControl UI control's right hand border to respond to a mouse drag. In other words make the control size bigger by grabbing the right hand boarder and dragging it to the right. I Have not been successful. So I am thinking if I incorporate a XAML file and create the ToolboxControl in XAML, maybe I would have more control in manipulating the ToolBoxControl.
I dont know how to break up a pure C# WPF code into C# and XAML.
In a nutshell, The problem I am dealing currently is that there is already C# code developed by other developers to perform certain function. I am using the existing C# code but I believe if I move some of the UI functionality from the C# into a XAML file, I would have a better handle on adding some of the needed UI features to the original program.
So you see my problem is I am not starting from scratch. I need to use an existing C# code and break away some UI functionality of it nd put it in a new xaml file. Do you have any ideas about any place that that has been done?
I am looking for any repsonse that leads me to a conversation thread or a sample. In other words I dont know where to start from. any useful guidance is apprecaited and would be marked as an answer.
Check out this article.
At its simplest, you will need to add an invisible (or not) drag handle to the right hand border of your control, then adjust the control size during the drag events raised by that drag handle.
Here is another article showing a similar concept expanded to an entire diagramming system, which includes the resize behavior you mentioned.
You may be able to define a style for what you are trying to accomplish. I wouldn't worry too much about breaking apart the C# from the XAML as that is a nice to have but not nec
I have a TabControl containing Prism regions. I want to trigger some kind of notification (e.g. flashing the tab header) for certain events, and I want to trigger this notification from the components in the Prism regions.
Once the tab containing such a component was open once, this works nicely using VisualTreeUtil.GetParent() and going up until I find my TabControl and can modify it to indicate the notification.
But the problem is that this doesn't work until the tab is opened by the user because VisualTreeUtil.GetParent() returns null; obviously because Prism doesn't hook up the visual tree until then.
Calling UpdateLayout() on the TabItem containing the region doesn't help. I don't want to open the TabItems programmatically, because this would confuse the user.
Is there anything I can do short of implementing a kind of region registry (which would be rather ugly and harder to maintain)?
If you want to look at code, I wrote a minimal solution to demonstrate the issue. The Print Structure button tries to go up the visual tree from the (initially unrendered invisible) hello TextBlock on the second tab. Before you switch to the tab it prints only the TextBlock, afterwards you get to the root of the visual tree. This is what I want to accomplish without switching to the tab.
Adding comment as answer:
This probably isn't exactly the answer you are looking for, but it seems like you're taking a very procedural approach.
Have you considered using an MVVM approach? Each TabItem in the TabControl can have a HeaderTemplate. In the template you can bind to a property in the ViewModel that causes the tab to flash or change appearance
Hm.. IIRC Prism regions are just a configured ContentControl/ContentPresenter. Once they are ready to work, all the bindings and datamodels should be in place, but the trees are left not created because they are invisible. If so, you should be able to call ApplyTemplate() on them to force it. I do not remember, however, if the Prism assigns the contenttemplates/datacontexts upon init, or upon demand - the latter may cause calling ApplyTemplate useless.
I have an idea for a personal project. And I know one way of accomplishing it in Windows Forms (which I no longer wish to use). Basically I could (in WinForms) just draw everything onto the screen (Form) and then when I need to switch views/states of the application, just redraw the new stuff in the old stuff's place.
But how can we have different states in WPF? Is there a "right" or "proper" way of doing this? Is something like this covered somewhere in the docs?
I'd like to do my own searching, but I have no idea what exactly to look for, and current attempts at finding the right information, so far have yielded no helpful (or even relevant) results.
Any help at all will be greatly appreciated. I am new to WPF, but have been making a lot of progress this past week!
Thank you!
P.S.:
I just thouhght of something. If the solution was to draw what is needed for one screen, and when it is time to display the next screen, just dispose of/hide everything and create/display the new stuff, then how would we get around this? Because we can't/shouldn't change XAML markup at runtime, can/should we? :/
Not sure how you drawn your views/states in WinForms (direct painting on a Graphics object?).
The closest to what you're describing is the VisualStateManager. You can use it to statically define several visual states inside a single XAML and transit between them (using a smooth animation if you want).
If what you've done was show different Forms with Show/ShowDialog(), then the equivalent would be to use different Windows and Show/Hide them.
If you just cleared/added Controls to your form, then you can do just the same in WPF. Most Controls in WPF have a Content or Children property instead of Control.Controls in Forms.
I don't know if I understand what you really want. But here are my thoughts:
You can use several Windows and Show/Hide them accordingly
You can use the Frame/Page functionality in WP (MSDN)
if you really need to you could load your XAML and remove the topmost content in your Window and replace it with the loaded content
You could use the VisualStateGroup functionality to change the appearance of your current window
I think you will be happy with the second solution
I am currently exploring the option of porting some older VB6 application to WPF with C#. The plan, in phase one, is to port several key forms and not all the application. The theoretical goal is to open the VB6 form in a container of some sort within WPF via an ActiveX dll.
Is this even possible?
I've tried looking at the Interop and can't seem to find a solid example of how get it to work with anything but Win32 controls, not a full form. I have full access to the old VB6 code and can modify it in anyway needed.
The following screenshot of the main WPF app would serve as the wrapper/container:
http://www.evocommand.com/junk_delete_me/main_menu_mockup.png
The current VB6 maintenance screen that would be loaded in the “white space” section on the right side of the previous screen.
I was able to accomplish the task with the following steps:
Created a new VB6 Active X Control Project. Copied and pasted the entire contents of the VB6 form controls and code behind into the new control. There are several elements that have to be handled in switching to a control:
you lose the ability to display
the caption of the form in the
previous manner. You can work around
it with alternate controls
(label/borderlesstextbox, etc) that
accomplish the same functionality if
needed. This wasn’t a priority since
each screen was being hosted in a
browser like tab system in our new
.Net project.
All mousepointer references have to
be changed from Me.Mousepointer to
Screen.mousepointer
You cannot use Me.Hide and have to
alternate events to hide the .Net
container.
Any and all references to
Me.[anything] have to be removed or
replaced with UserControl.[anything]
if they are applicable.
If you use any functions that
reference a
[yourcontrol].Contianer.Property on a
form they will need to be altered to
loop through the UserControl.Controls
collection instead and “Container” is
invalid for vb6 ActiveX controls
All non-modal forms/dialog boxes
must be removed from the project as
there is now no Hwnd to handle in WPF.
You get an error of 'Non-modal forms
cannot be displayed in this host
application from an ActiveX DLL,
ActiveX Control, or Property page'.
In our case we had a simple splash
screen that would display when certain
long processes/reports displayed to
let the user know what was running.
I was unable to directly add the VB6 control via the interop to a WPF project . As such a new .Net “Windows Form Control Library” project was created. A reference to the VB6 OCX was added to the project. The VB6 Control s were then added to the .Net toolbox by “right click” –> “Add Item” and pointing a com reference to the VB6 control ocx. The .Net control was then used to host/serve the VB6 Control.
To display host a form in the VB6 and get it to fire the necessary initialization functionality the VB6 OCX controls were defaulted in a Visible.False manner so they were initially added to the .Net OCX as invisible controls. When needed the VB6 control is set to visible = True which fires the UserControl_Show() event. All code formerly in Form_Load() was moved to this event. The show event was the easiest method of accessing the Form_Load as needed. MSDN: “The control does not receive Show events if the form is hidden and then shown again, or if the form is minimized and then restored. The control’s window remains on the form during these operations, and its Visible property doesn’t change.”
Wrapping the vb6 controls within a .Net Winform control resolved the issue with Radio/Option buttons being rendered as black as outlined elsewhere in one of my responses to this question without having to convert the frames to Picture boxes as suggested.
In the WPF app as a menu choice is selected xaml code is dynamically created and displayed via a wrapper with a WindowsFormsHost tag. A dynamically created control object from the .Net winform app is then pushed into the WindowsFormsHost tag on the xaml and the control is made visible on the .net project which fires vb6 UserControl_Show and then load and display of the vb6 form.
I think what you will have to do is extract the VB6 form contents into an ActiveX control. You can then expose this in your ActiveX dll and place that in your WPF form. I doubt it's possible to host a VB6 form within any other type of form.
Can you even load that VB6 form in another VB6 form? I suggest you get that working first.
There is no reliable way to set parent of a VB6 form. You can always hack it or use plain ActiveX control (UserControl in VB6) as UI container instead of VB6 forms.
I found a method to do what was needed within WinForms rather than WPF at this point.
http://www.codeproject.com/KB/vb-interop/VB6formsinNET.aspx
I figure if I can get it working 100% there I can port it over to WPF or worse case host the WinForm element in the WPF form if I absolutely have too (U-G-L-Y).
Anyway, I've gotten a bit closer, but am having a very odd issue with certain controls painting too the screen. Radio/Option buttons are rendering as solid black:
http://www.evocommand.com/junk_delete_me/optionbuttons.png
I've tried explicitly changing the controls' background color from buttonface to a fixed color and it still does it. I'm assuming it's a layering issue with the option buttons being within the frame control. I'm at a bit of a loss on how to proceed without massive rework of the VB6 content to change the options buttons to checkboxes. It's a hefty app and there are 600+ option button controls across the application that I don't exactly want to deal with.
EDIT:
I was able to confirm it has something to do with the layering of the option within a Frame control. If pulled out to the base form the issue does not occur:
http://www.evocommand.com/junk_delete_me/optionbuttons2.png