Monday, 27 February 2017

Fully tailorable form text (wpf)

How do you get fully tailorable text on a window using wpf (c#,xaml)?

Painfully!

There are functions built into the system by MS that are designed to deal with localisation problems but this is at the dll level.  What I need for ossAccounts is a simple method for the vertical market developer (or systems admin) to be able to edit a table and change any field, either to allow additional languages or just tailoring the solution.  For example some accounting systems use 'Nominal Ledger', others 'General Ledger' etc.

It is particularly problematic when working with datagrids since the headers can not be simply bound.

The following is using a modified version of the GenericCRUDControl by Abdulrahman Emad and  Muhammad Magdi (found here ) but should transfer to other grids easy enough.  While there may be other approaches (if someone knows a better one, please tell!) it does work.

<CustomDataGridColumn BindingExpression="Name" Width="Auto">
       <CustomDataGridColumn.Header>                
                <TextBlock Text="{Binding DataContext.F[Name], 
                                                     RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},
                                                     Mode=OneWay}"/>                
        </CustomDataGridColumn.Header>
</CustomDataGridColumn>

Where F[Name] is :-

public Dictionary F { get; set; } = new LocalisationDictionary();

on the DataContext class (in the above on the CRUDWorkspaceViewModel). Note that the way I am handling multiple windows means that the ancestor I need for the DataContext is a UserControl, yours may be different.

LocalisationDictionary then returns the appropriate text based on the string it receives, the users language etc.

public class LocalisationDictionary : Dictionary
{
     public new string this[string key] => "Foo_"+key; // Some method here.
}

No comments: