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

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).

Related

using custom controls instead of user controls to create complex views

I do not have enough information about WPF, so please correct me.
It seems that to handle different views create many usercontrols is needed(each view needs one usercontol which binds to the viewModel) , and also by using MVVM pattern designers can create views independently.
now if the designer tries to create two themes with different structure, he has to create two usercontrols because when using usercontrols the layout is specified(as mentioned here).
on the other way customControls do not specify the layout, so it seems that using CustomControls is more reasonable.
so the question: is using custom controls instead of usercontrols is correct, and if it is, is it reasonable for viewmodels to inherit from Control, and views become only styles for viewmodels?
Unless you need the functionality provided by custom controls, I would suggest using UserControls or DataTemplates. They are simpler. Here's a related question\answer.
WPF User Controls vs Custom Controls

Binding winform control property in XAML

We are currently in the process of switching our product from WinForms to WPF. At the moment we are using some 3rd party WinForm controls that are required for our application. Even though we plan to eventually replace them with WPF versions, right now this is not possible. We've tried hosting them in the wpf window inside WindowsFormsHost control, and it seems to work just fine. The only problem we have is how to pass our data from VM to these controls. We would like to avoid any code-behind and alterations to VM just to accomodate this controls. Ideally, we would prefer to keep VM completely unaware of the controls used to display it's data, so that when we do change to WPF version of these controls, we only need to modify the view. This is why we're looking for a way to bind VM property to hosted WinForm control from XAML. If this helps, we can certainly live with the fact that there is only a one way binding from VM to the control, and we don't mind if the binding works only once, without the subsequent updates from VM, since the VM properties we are binding do not change. Perhaps someone has any ideas how we can make this happen?
Not sure if there is a better way, but here's one idea:
Wrap your WinForm control/WindowsFormsHost control into a wrapper control (inherit from Control or use a UserControl, whatever is best for you).
On this wrapper you can add dependency properties that you want to bind to your VM.
Inside the wrapper code, you can add the boilerplate required to propagate changes back and forth between your wrapper dependency properties and your winform properties.
This hides the dirt under the carpet and exposes a nice WPF facade that you can bind to as usual, without changing your VM.
When the control is phased out, remove the wrapper from your project and you can bind the VM directly to the new WPF replacement control.

PasswordBox alternative for 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.)

Common shared views. Views + ViewModels or UserControls?

I am developing a little utility view that will be embedded in several of our apps. It will sit in a common library.
Should I expose this as a ViewModel along with a default View implementation, or would it be better as a UserControl with a fixed GUI?
It is pretty self contained and I doubt it will need to be reskinned, but doing it as a UserControl seems a bit overkill with having to set up a load of dependency properties.
A simple ViewModel seems more attractive to me but wondered if this was the normal way of sharing stuff?
EDIT:
It would also be nice if I could embed this in WinForms apps too. Is this possible with View/ViewModel?
Well, in the end I went with View/ViewModel. This keeps the separation nicely and is easily pluggable into existing MVVM projects.
It also works fine in WinForms, given that a View is just a UserControl with its DataContext set to some arbitrary object (the ViewModel).
The only slight issue I had was the fact that Application.Current is not set in a forms environment, so I had to store the GUI dispatcher reference so I could marshal gui updates to the proper thread in my ViewModel.

Difference in creating Application object in WPF and WinForms

Why is there a difference in the way the Application object is created in WinForms and WPF?
-> In WinForms we never created the Application object. It was always available (I believe it was Singleton pattern). In WPF, although hidden in App.g.cs we need to instantiate one.
-> In WinForms it was a sealed class, but in WPF the way to go is to inherit it.
Is this done:
to be able to define the application in Xaml (App.xaml)
due to introduction xbap/navigation projects?
What benefits does it really provide?
I don't know that the design decision is entirely motivated by the desire to be able to define the Application object in XAML. But that's reason enough, it seems to me.

Resources