I'm really new to WPF and I'm trying to change the hover colour of a button in WPF. I've figured out I need to create a control template in order to efficiently do this, which I've been able to successfully do in a standard WPF application which has a App.XAML file, however the application that I'm using isn't a full WPF app, it's a winforms app that uses a ElementHost to link a WPF user control into the form. Soooooooo I was wondering how do I create a control template for WPF user control? I don't have a app.XAML which is where I put the control template the first time I did it, and if I try to slide the control template into any of the user control XAML it throws an error.
Thanks
When you're creating the template in App.xaml you're adding it as a resource by putting it inside the Application.Resources resource dictionary. You can do the exact same thing on any other element in XAML that represents a FrameworkElement (i.e. any control, layout panels, etc.). The basic setup is like this:
<UserControl ...>
<UserControl.Resources>
<ControlTemplate x:Key="MyCustomTemplate" .../>
</UserControl.Resources>
</UserControl>
Related
I'm struggling with Templates in WPF
I understand the concept in that I can have a control, and 'override' the ContentTemplate (or similar). I use it often with the TabControl
However, I don't understand it in terms of what the initial control looked like. And if we are limited on the names. EG, could we build a control and overwrite the NonsenseNameTemplate?
If I were to build my own UserControl and provide the ability for people to override things like my NonsenseNameTemplate, what would this code look like?
My guess, with pseudo code would be
<UserControl>
<NonsenseNameTempalte>
//some resources
//some other controls
</NonsenseNameTempalte>
</UserControl>
In the above example, I can fully understand how I could create a new control and overwrite the NonsenseNameTemplate but I can't see any code examples of the UserControl and it's usage.
Template is nothing more than a (dependency) property. By writing
<Button>
<Button.Template>
<ControlTemplate>
...
</ControlTemplate>
</Button.Template>
</Button>y
you are doing nothing more than creating a new instance of ControlTemplate class and assigning it to Button.Template property of that specific button. Each control has it's own ControlTemplate saved somewhere in it's assembly. It is generally not so simple to actually get those default templates, but Blend can help with it.
Of course, you can create your won NonsenseNameTemplate property, but actually using it would require some deep knowledge of WPF composition, layouting and rendering. Which is usually not required for normal usage of WPF. And I agree that there is not a much online resources about doing something like this, for exactly this reason.
I too had an early on learning of Templates. I posted a step-by-step answer to another question via a customized button control. The nice thing about that sample, you can build and play with styles and templates in a small project and see visual impact without requiring full project rebuild
To start with, as you have mentioned that you do not have Blend. You can have Blend and install it with Visual Studio 2013 Community.
You can download this here if you do not have it already.
http://www.visualstudio.com/products/visual-studio-community-vs
A part of the installation process, it allows you to select and install Blend. Also, the newer editions of visual studio give you some of the power of blend. In your design view you can right click on a control and create or copy a template.
On to the question.
As Euphoric has mentioned. Custom control authoring does require a little more in-depth knowledge of WPF, or any of the XAML frameworks. However, there are Visual Studio templates that can help you in the right direction.
As for the template naming, you have three types of templates you will come across in XAML. ControlTemplate (which for your purposes is the one we are interested in), ItemsPanelTemplate and DataTemplate. Again, as Euphoric has said, there are few circumstances where deriving a custom version of any of these three templates would bring anything to the table.
If you were to create a test WPF application, once you have created the basic project and solution. Go a head and add in another project, and from the templates VS provides, File -> Project -> New -> Windows Desktop. In the project template list, find 'WPF Custom Control Library'. Once created, reference this project in your main WPF project.
This custom control library project will give you a skeleton setup for what you are looking for.
If you look in this project here are some things for you to note.
Firsty, you will find a folder called Themes and in there a file named Generic.xaml. In there you will see a style that has a setter setting the Template property. You will also see that both the Style and set ControlTemplate have a set TargetType that should be local:CustomControl1.
This is important as this is showing you how to create a custom controls default template. Now, to apply this template look in CustomControl1.cs and consider the following code.
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
For this control named CustomControl1, that template we looked at in the xaml will be automatically set as that controls default template where ever that control is used.
This project is a good starting point. But now you may want to override this ControlTemplate inside your main project. This is simple. I have code that looks something like this inside my main WPF project.
At the top of the MainWindow.xaml
<Window x:Class="CustomControlTesting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CustomControl="clr-namespace:CustomControlTesting.CustomControlLibrary;assembly=CustomControlTesting.CustomControlLibrary"
Title="MainWindow" Height="350" Width="525">
And in the body,
<CustomControl:CustomControl1>
<CustomControl:CustomControl1.Template>
<ControlTemplate TargetType="{x:Type CustomControl:CustomControl1}">
<Grid>
<!-- Define my look to override the template -->
</Grid>
</ControlTemplate>
</CustomControl:CustomControl1.Template>
</CustomControl:CustomControl1>
This is a brief overview and certainly misses out a lot. But I hope this is of help to you and can get you started.
I've looked at using a control template to use the same XAML code between two views. However, where do I store this Control template so both views can access it and therefore display the controls I need?
Thanks
If both views are under same window, you can store ControlTemplate under Window.Resources and if both view belongs to different windows store that under Application.Resources.
I have a WinForms User Control (a toolbar) which i would like to add on a WPF user Control, is there any way to do this?
like i want a WinForms User Control (the toolbar) along with other WPF Controls
(datagrid) on a new WPF User Control
I saw a couple of samples that show Windows Forms hosted in a WPF Control. But that is not what i want to see.
This is pretty easy to pull off. There is a handy little thing known as WindowsFormHost all you have to do is declare it in your control's XAML, and nest your forms control inside of it, like so:
<UserControl>
...
<Grid>
<WindowsFormsHost>
<forms:MyFormsToolbarControl/>
</WindowsFormHost>
</Grid>
...
WindowsFormHost lives inside of the normal WPF toolbox so it shouldn't be hard to locate. Meanwhile there is an example of how to produce the equivalent XAML in code at this location...
http://msdn.microsoft.com/en-us/library/ms751761.aspx
You can check out my answer to a similar question here:
WPF hosting a WinForm, Tab Navigation problems
This will also show you how to fix a tabbing issue with windows controls that are sitting inside wpf views.
How do I configure a WinForms TabControl inside the WindowsFormsHost WPF control, how can add TabPages using XAML?
Thanks.
So far as I understand it, you can create the WinForms TabPages in the same way you would create something normally in WPF with XAML. See this page for an idea of what I mean.
We solved encapsulating the TabControl and its tabpages inside a UserControl, then adding the UserControl to the WindowsFormsHost.
I'm hosting an ActiveX control in WPF form. It works well, no problem.
But I can't find the property that sets the location of the ActiveX control on the WPF form (in the code). So it's stuck at the center of the screen for now.
the host is a System.Windows.Forms.Integration.WindowsFormsHost
Layout of controls in WPF is done through layout panels like Grid, StackPanel, DockPanel, etc. They all layout the controls they contain different, so it depends on what you want. The xaml code below uses a stackpanel which layouts the controls below each other.
<StackPanel>
<WindowsFormsHost ... />
</StackPanel>