PasswordBox alternative for WPF - wpf

I just ran into a limitation with WPF that makes the current PasswordBox unusable. I must create a derive control to implement some interfaces that my UI needs.
What that said, is there another control out there that serve the same purpose and is NOT sealed? Opensource possibly?

The general approach to controls in WPF is to compose (or restyle) rather than inherit. As such, if you want to create wrapped behavior around a PasswordBox, one option is to derive a new UserControl containing a PasswordBox, then implement the interfaces on your new UserControl. If the overhead of a UserControl is too high (it shouldn't be in most cases; it's a lot lighter than the old WinForms User Control concept), you can derive from FrameworkElement directly, though this is a bit more work (have to override a few methods to forward the Arrange/Measure steps and add the PasswordBox to the visual tree.)

Related

Which element to use to modularize WPF applications?

Lets say I have a search box, which is a stack panel containing a TextBox and a Button with an icon. For easier re-usability I would like to extract said search box into a separate file.
What would I use to wrap the searchbox? I have all the functionality as attached behaviors, so I don't need any code behind.
ItemsControl doesn't fit, because I don't want to display items, ContentControl does not, because I have no content...
Could you give me hints how to fragmentalize in XAML? The only examples I find are for ResourceDictionarys, but not everything is a Style.
User Control
User Controls provide a way to collect and combine different built-in controls together and package them into re-usable XAML. User controls are used in following scenarios:
If the control consists of existing controls, i.e., you can create a single control of multiple, already existing controls.
If the control doesn't need support for theming. User Controls do not support complex customization, control templates, and difficult to style.
If a developer prefers to write controls using the code-behind model where a view and then a direct code behind for event handlers.
You won't be sharing your control across applications.
Custom Controls
A custom control is a class which offers its own style and template which are normally defined in generic.xaml. Custom controls are used in the following scenarios:
If the control doesn't exist and you have to create it from scratch.
If you want to extend or add functionality to a preexisting control by adding an extra property or an extra functionality to fit your specific scenario.
If your controls need to support theming and styling.
If you want to share your control across applications.
source (including example and more)
What would I use to wrap the searchbox?
A UserControl: https://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol(v=vs.110).aspx
Creating a UserControl is a suitable model if you want to build your custom control by adding existing elements like for example StackPanels and TextBoxes to it.

Best way to set CurrentCulture so it applies to all WPF components

I've just learnt to my surprise that WPF doesn't use the CurrentCulture for bindings, instead defaulting to en-US.
In a pure WPF application, this can be fixed in one place by setting the language globally once in the App class.
However I have a WinForms application that is being progressively migrated to WPF, and contains several WPF UserControls. What's the best/simplest way to ensure the CurrentCulture is used for all UserControls? Do I really have to make all my UserControls inherit from a base class that does this, or is there some way to set it globally?
You can use a slightly different approach and derive once from ElementHost and manipulate your WPF UserControl instances as they are instantiated. For example, you can create a LocalizingElementHost with a ChildChanged event handler that does to the child what you would have done in a base class.
You can still use the same approach with LanguageProperty.OverrideMetadata, just put it at the beginning of your program (Main method).

How do I use derived controls in my Windows Forms application

At best, I am a UI novice, but I am a pretty good OO developer (if I don't say so myself). I am confused as to how to work with the designer. I am developing a Windows Forms application. I have a control (TabPage) that I derived from System.Windows.Forms.TabPage that I want to place on a System.Windows.Forms.TabControl control.
public class MyTabPage : TabPage
{ ... }
I am not sure how to get the designer to use my control.
A fellow developer looked at this for me as suggested that I add more information.
I've prototyped a WinForms application. The application has a TabControl, with three tab pages on it. Each TabPage has a SplitterControl with a DataGridView control in each half of the splitter. The two DataGridView controls form a master-detail view of some data we need to edit. I've added a bunch of event handlers to each DataGridView control and each time the handler code is placed in the form. The form is getting pretty crowded and would like to use OO techniques to clean things up.
I would like to redo the prototype now and because each TabPage is behaviorally identical, I would like to use OO to abstract away some of the complexity. This is where my lack of experience with the designer comes in. There must be a way to use the designer for something more complex than my origonal prototype.
You aren't really changing the behavior of the TabPage, it's just a container after all :). The TabPage and TabControl are rather tightly coupled, and they probably should be. Your "control" is what should be placed on the TabPage. So define your user control as a panel with a splitter and two datagridviews and then just drop your user control on each TabPage in your app. The Tab Control/Page tandem will still work automatically and your custom code will be in the appropriate control.
Unfortunately, there's no real practical solution to your problem. You'd have to create a custom TabControl as well and give it its own designer so that it will create instances of your derived class instead of the default TabPage class.
Sadly, the TabControlDesigner class in System.Design.dll is internal and can't be derived from. You'd have to write a complete replacement for it. That's difficult, it is a pretty advanced designer. You could have a look-see with Reflector to find out what it takes.
UserControl
Simply create a user control that contains your control. Then it will be available in the ToolBox window of Visual Studio.
For more details, Understanding the User Control Designer...
A User Control is similar to any other
class, but with the added ability to
be placed in the Toolbox and displayed
on a form.
The tutorial differs a bit from Visual Studio 2010 but I'm sure you will get through it.
TabPage Control
As for the TabPage control, you can't use it stand-alone in the designer. It must be part of a TabControl.
The TabControl is built to contain TabPage controls. If you use the designer to add/remove any TabPage, in the back-end it does "new TabPage", it can't determine that it needs to create a new instance of your derived class of TabPage.
Other Suggestion
A TabPage is just a container, I can't figure out why you need to override the TabPage control. I know you have added some information to your question but you may have to revisit it again so we can better understand.
Since the TabPage is a container, you should create a UserControl which contains your SplitContainer and DataGridViews. Therefore you would be able to reuse that UserControl on each TabPage.
The designer is just a complement to UI programming, it is not mandatory. You can add a UserControl to a TabPage without using the designer.
I may be off the track here, if so please provide more details. A visual prototype of what you need may help.
It's been a while, but I thought I would answer this question.
I was able to get the OO implementation I wanted by splitting the control up. The control is implemented pretty much the same as the prototype. I then created a control binder class hirearchy with derived classes to contain the specific behavior(s) I needed. The control has a reference to the binder base class and calls binder functionality as needed. The derived binders override properties and methods as necessary. Pretty much a textbook OO solution.
Where I was stumbling was in thinking that I needed to have a control hirearchy.
Thanks everyone for your help. I was under some pressure when I asked this question. It's amazing how much I've learned in the months since.

MVVM/Presentation Model With WinForms

I'm currently working on a brownfield application, it's written with winforms,
as a preparation to use WPF in a later version, out team plans to at least use the
MVVM/Presentation model, and bind it against winforms...
I've explored the subject, including the posts in this site (which i love very much),
when boiled down, the main advantage of wpf are :
binding controls to properties in xaml.
binding commands to command objects in the viewmodel.
the first feature is easy to implement (in code), or with a generic control binder, which binds all the controls in the form.
the second feature is a little harder to implement, but if you inherit from all your controls and add a command property (which is triggered by an internal event such as click), which is binded to a command instance in the ViewModel.
The challenges I'm currently aware of are :
implementing a commandmanager, (which will trigger the CanInvoke method of the commands as necessery.
winforms only supports one level of databinding : datasource, datamember, wpf is much more flexible.
am i missing any other major features that winforms lacks in comparison with wpf, when attempting to implement this design pattern?
i sure many of you will recommend some sort of MVP pattern, but MVVM/Presentation model is the way to go for me, because I'll want future WPF support.
Thanks in advance,
Erik.
Please take a look at Update Controls .NET. It is an open-source library for Winforms, WPF, and Silverlight that keeps controls up to date as data changes. You can start using it now for Winforms, and then transition over to WPF without changing your Data Model or View Model code.
Unfortunately, it does not solve the Winforms command binding problem. Your button click events will not port from Winforms to WPF. But it does take care of the data binding problem.
You might find the WAF Windows Forms Adapter interesting. It shows how to apply the Model-View-ViewModel (MVVM) Pattern in a Windows Forms application. The Adapter implementation provides a solution for the missing Command support in Windows Forms.

WPF Interop & Dialogs

I have an existing WinForms application for which I'm now designing new bits in WPF. Things are going reasonably well and I have run into my first need for a dialog.
I'd like to do the dialog in WPF. It appears as though I'm going to need to do a UserControl for the actual content and then host that content via a WinForms form with an ElementHost (since UserControl has no ShowDialog() method).
And that's where my question is. How does that work? Best I can tell, the WPF UserControl doesn't even have a DialogResult property (which makes sense given that it has no ShowDialog() method) - it looks to me like I'd need a WPF Window control - and I don't think I can use that in this case.
Struggling with the basic flow and setup of things here. Can someone shine a light?
Is this even possible?
You can open a WPF window from a WinForms application.
Just create the window and call ShowDialog(). The CLR will load the WPF framework and open the window.
If you want your interop application to work mostly like a WinForms app, then the approach you describe works fine -- I've pretty much the same thing in my interop cases.
WPF supports MessageBoxes (albeit a slightly different version than WinForms), and you could put something together using WPF Windows (extending it by adding something similar to DialogResult). However, the provided WPF controls suggest that they're trying to change UX interactions to minimize dialogs, particularly modal ones.
To make your life easier though, I would create a WinForms Form/ElementHost subclass specifically for dealing w/hosting WPF content, and depending on how clean you like your "using" declarations, wrapping your own DialogResult-like enumeration so you don't have to include the System.Windows.Forms namespace which can make your WPF code-behinds more cumbersome.

Resources