Click here to Skip to main content
13,632,490 members
Click here to Skip to main content
Add your own
alternative version

Stats

2.9K views
85 downloads
9 bookmarked
Posted 25 Jun 2018
Licenced CPOL

Creating Custom WPF Derived Control with Attached XAML File

, 25 Jun 2018
Rate this:
Please Sign up or sign in to vote.
A way to create a custom derived control with the XAML file attached like a UserControl

Introduction

I have created quite a few derived custom controls that include XAML. The problem is how to keep the control code and XAML together.

One approach is to use the Generic file, but this means that there the code is in one place and the XAML in a totally different place, and normally have a single Generic file in the solution. Also, you will get to have a very large XAML file unless you start to break it up. If you do not break up this file, things can become very unmanageable very quickly.

You can also put the XAML in a ResourceDictionary such as app.XAML or a separate XAML file referred to in the app.XAML file, or in the Window or UserControl XAML files that use, but again this separates out the code and the XAML, and again should probably move the XAML for different controls into separate files. You can still associate the code and XAML by having them in the same folder, with similar names, but then this has to be referenced in with a MergeDictionaries in XAML. You also have to be careful with naming because if the code file and XAML file have the same name, this confuses Visual Studio and you get unintended results.

Solution

I came up with another way to do it. Basically, a UserControl is created and then change "UserControl" to the name of the control I want to derive from both the XAML and code files. Then in the constructor in the code file, there is the following:

public StyledComboBoxControl()
{
    InitializeComponent();
    var style = (Style)FindResource("Styling");
    Style = style;
}

Then finding the style resource has to be done after the InitializeComponent, it will not work before. The rest of the code behind contains whatever code is needed for the control to function correctly. In this case, it is only DependencyProperty definitions for all the extra properties this Control has beyond what is normally associated with a ComboBox.

The XAML file consists of only the Resources section for the Control, which contains a ResourceDictionary which contains the Style with an associated Name attribute so that is can be found with the FindResource method.

Using the Code

Using this control is just like using any control in WPF:

<local:StyledComboBoxControl Width="300"
                             VerticalAlignment="Center"
                             ItemsSource="{Binding Items}"
                             Label="Sample Combobox"
                             SelectedValue="{Binding SelectedValue2}"
                             SelectedValuePath="Key" />

History

  • 2018-06-25: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Clifford Nelson
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionYeah I quite like this idea actually Pin
Sacha Barber26-Jun-18 3:37
mvpSacha Barber26-Jun-18 3:37 
BugMissing sample? Pin
Tencor Developer25-Jun-18 10:21
memberTencor Developer25-Jun-18 10:21 
AnswerRe: Missing sample? Pin
Clifford Nelson25-Jun-18 10:22
mvpClifford Nelson25-Jun-18 10:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180712.1 | Last Updated 25 Jun 2018
Article Copyright 2018 by Clifford Nelson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid