<Application.Resources>
<SolidColorBrush x:Key="UserBackgroundColor" Color="White"/>
<SolidColorBrush x:Key="UserControlColor" Color="White"/>
<SolidColorBrush x:Key="UserControlFontColor" Color="Black"/>
<FontFamily x:Key="UserControlFont">Segoe UI</FontFamily>
</Application.Resources>
Let's assume we have a user object which remembers the customizations made by the user. When the user logs in his settings are loaded and all elements referencing the resourcedictionary we defined earlier will display the way the user defined.
private void MergeDictionaries(User user)
{
ResourceDictionary resourceDictionary = new ResourceDictionary();
SolidColorBrush userBackgroundColor = new SolidColorBrush(Colors.White);
if (user.BackgroundColor != null)
{
userBackgroundColor = user.BackgroundColor;
}
resourceDictionary.Add("UserBackgroundColor", userBackgroundColor);
SolidColorBrush userControlColor = new SolidColorBrush(Colors.White);
if (user.ControlColor != null)
{
userControlColor = user.ControlColor;
}
resourceDictionary.Add("UserControlColor", userControlColor);
SolidColorBrush userControlFontColor = new SolidColorBrush(Colors.Black);
if (user.ControlFontColor != null)
{
userControlFontColor = user.ControlFontColor;
}
resourceDictionary.Add("UserControlFontColor", userControlFontColor);
FontFamily userControlFont = new FontFamily("Segoe UI");
if (user.ControlFont != null && !user.ControlFont.Equals(""))
{
userControlFont = new FontFamily(user.ControlFont);
}
resourceDictionary.Add("UserControlFont", userControlFont);
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}
Every control referencing the resourcedictionary will display itself differently according to which user is logged in.
<TextBlock Name="txtText"
Foreground="{StaticResource UserControlFontColor}"
FontFamily="{StaticResource UserControlFont}"
Text="Text"/>
Geen opmerkingen:
Een reactie posten