Organizing Generic.xaml

Creating controls in WPF seems difficult at first. The separation between code and the appearance of the control is one of the great WPF features, but it requires a different way of thinking about controls. The code is located in its own folder and namespace, but the control default style is defined in the Generic.xaml file (or one of its themed cousins) that is located in the project’s Themes directory.

Although the behaviour (code) and its appearance (style) are completely separated, you still work on both of them at the same time. So it would be convenient to have these files close together. Another problem of the Generic.xaml file is that it tends to get very large, when you have a lot of (complex) controls. Fortunately, there is a good way to solve this issue.

Default styles need to be defined in the Theme\Generic.xaml (or one of its themed cousins). Nothing you can do about that, but you can make the generic resource dictionary a merged dictionary that loads other dictionaries that you can put on arbitrary places.

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/RamonDeKlein.Controls;component/AsyncLoadControl/AsyncLoadControl.xaml"/>
        <ResourceDictionary Source="/RamonDeKlein.Controls;component/AnimatedSizeControl/AnimatedSizeControl.xaml"/>
        <!-- include other definitions here -->
    </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

You can create separate resource dictionaries for your controls and put them in the same folder as your code’s control. Make sure you set the build action for the resource dictionary to ‘Resource’.

A drawback of this solution is that you need to specify the assembly name for each resource dictionary that you want to merge. This is a flaw in WPF when using merged dictionaries in Generic.xaml.

2 thoughts on “Organizing Generic.xaml

  1. Thanks for posting. It’s interesting that I have read it in several places about setting build action to Resouce for the ResourceDictionary xaml file. However, based on my testing, it doesn’t work if I set the build action to “Resource”, but it works fine with the default setting of “Page”. What did I miss?

  2. Sir,
    I want to update cell in WPF Datagrid.

    To do this Stack-Overflow gives the example of DataGridEx to execute updating.

    public void HandleMainDataGridCellEditEnding(
    object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
    {
    if (!isManualEditCommit)
    {
    isManualEditCommit = true;
    Microsoft.Windows.Controls.DataGrid grid = (Microsoft.Windows.Controls.DataGrid)sender;
    grid.CommitEdit(Microsoft.Windows.Controls.DataGridEditingUnit.Row, true);
    isManualEditCommit = false;

    MessageBox.Show(String.Format(“HandleMainDataGridCellEditEnding: {0}”,isManualEditCommit));

    }
    }
    What must I write in xaml windows.resources?

    ????

    You have any link?
    Thanks in advance.

    N.Franken
    The Neherlands

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>