Using this tool you can create your styles, just in case you don't have the expert designer, you always wished you had, in your team.
Will try to create to my own and post it here soon :-)
<Application x:Class="StyleSelector.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="BureauBlack.xaml"></ResourceDictionary>
</Application.Resources>
</Application>Results:Without style![]()
With:![]()
(there is some problem with the Style, it doesn't set the Window background color)
Setting style in code Now let's try to set the style of a window in code. Here is a method which takes in a XAML file name (containing styles and added to project). We create a new ResourceDictionary object and set it to the Window's Resource collection.private void SetStyle(string xamlFileName) { //clear the resources this.Resources.Clear(); if (!string.IsNullOrEmpty(xamlFileName)) { //load new resources ResourceDictionary resDict = new ResourceDictionary(); resDict.Source = new Uri(xamlFileName, UriKind.Relative); //merge the resources to preserve any other resource you may have added this.Resources.MergedDictionaries.Add(resDict);//or to overwrite and avoid memory leak you could also do this//this.Resources = resDict;} }We can use this method in the "Set Style to:" Combo Box selection changed event.
In one of projects I am working on, we have a requirement of showing ‘recent files’ used by the application. The files used by the application are XML files – no custom extension.
Our goal is:
For this task we need the following things:
- a RecentFiles class:
- should contain the list of recent files
- should have methods to load/save the list to disk (persistence)
- should have methods to add a recent file used or delete a non-existing file
- a MenuItem (with children) to show the list
Here is the RecentFiles class:
public partial class RecentFiles
{
private ObservableCollection<string> filesField;
public ObservableCollection<string> Files
{
get
{
return this.filesField;
}
set
{
this.filesField = value;
}//members for persistence – Load, Save
//you can find the full class definition in attachments
}
In the Windows.cs, we have a view model WindowViewModel having a property named AppRecentFiles.
Code in WindowViewModel:
public RecentFiles AppRecentFiles { get; set; }
Code in Windows.cs:
this._mainWindowViewModel = new MainWindowViewModel();
this.DataContext = this._mainWindowViewModel;
Now the XAML part:
<MenuItem x:Name="mnuRecentFiles" Header="_Recent Files" ItemsSource="{Binding AppRecentFiles.Files}">
<MenuItem.Resources>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding ElementName=mnuRecentFiles, Path=DataContext.RecentFileChosenCommand}" />
<Setter Property="CommandParameter" Value="{Binding}" />
</Style>
</MenuItem.Resources>
</MenuItem>
And you are done with the basic infrastructure.
Now all we have to do is:
- add an item to the AppRecentFiles.Files list whenever we load a new file (from the Load option in menu). Tip: add the new item at top of list so that it shows as first item on the menu.
- remove file from list if the user selects a file from 'recent files’ is not found, perhaps after a confirmation message box
- load the recent files by using RecentFiles.LoadFromFile() method – usually done during Window load or app start
- and save (persist) the list by calling RecentFiles.SaveToFile() on App close or window close
Post with links to what I am reading: 1. A very good post on different aspects of system architecture: https://lethain.com/introduction-to-a...