I recently found myself wishing the the toggle button on the Expander control was on the right hand side rather than the left.
I personally use Kaxaml for such purposes. WPF applies the default control template for the current theme. A Style can set any of the public properties of a control. Of course the same public properties can be set in the control declaration itself, but the use of styles facilitates reuse and helps achieve a common look and feel across the application.
Styles are typically declared as static resources in a window or application resource dictionary. Styles may be applied to a control either explicitly or implicitly. Each style declaration must specify to which control type it applies, and it may only apply to one type. This Style changes the Expander to be visually more to my liking, however the toggle button is still on the left hand side.
I want an Expander with the button on the right.
There is no Expander public property to move the toggle button. Examination of the SDK sample shows that a control template must be used. I modified the SDK sample to my own liking. I had to cut much of the code out because it is too much to list here. Download this XAML code here to see it all. Notice how the expander control template has an outer grid and an inner grid. The outer grid has two rows, one for the header and one for the content. The content row has a height of zero and is thus not visible by default.
The header row contains another grid with two columns. The header content is placed into the left-hand column and the toggle button into the right. The button is now where I desire it to be. Like the expander they are using in the properties windows? You are commenting using your WordPress. You are commenting using your Google account. You are commenting using your Twitter account.
You are commenting using your Facebook account. Notify me of new comments via email.If you need to change only the icon that is used to expand an item, you can use the ExpanderStyle property of the RadTreeView. The icon is a ToggleButtonso any style that can be applied to a ToggleButton can be applied to the icon too.
On the snapshot below you can see how the final result will look like:. Since the expander is a toggle button we need to create a style with TargetType - ToggleButton.
Declare a new style in your application user control resources and set the following common properties:. Next, we need to change the Template property of the ToggleButton Expander. Add the following elements to your Template:. We are one more step closer to the final result.
Next we should add some visual effects. For that purpose add the following elements to your expander template:. The first one is activated when the mouse is over the expander, and the second one is activated when the ToggleButton is checked:. With the same success and effectiveness you could use Expression Blend for adding interactivity.
Since the operations in this example are pretty simple I decided to write the triggers by hand. Here is the result:. All Rights Reserved. See Trademarks for appropriate markings. UI for WPF. API Reference. In this article. Not finding the help you need?
Customizing WPF Expander with ControlTemplate
Contact support. Was this article helpful? Tell us how we can improve this article. Submit Skip.I recently needed to style a WPF expander control for a prototype project. But, I also needed to change the font used in the expander header. Ok, no problem — just use a setter in the style like this - right? Bzzzztttt… wrong. That set the font appearance in the Expander header like I wanted, but all the controls in the Expander content property inherited the font appearance.
So, i dug around in MSDN a little more.Intro to WPF: Learn the basics and best practices of WPF for C#
The key information turned out to be in the documentation for the Headered Content Control class. This is an important class as it defines the base functionality for controls including the Expander, Group Box and TabItem controls — all very commonly used controls. They key to getting the look I wanted is using Data Templates. In short, data templates are like control templates but instead of defining how WPF structures the visual tree for a control, data template defines how WPF displays data.
Ya, Duh! All data template intentions are bound to a property on an object instance. You can see how this is used below. The key is that all Headered Content Controls have a data template for the header content. So, setting the font appearance for the Expander header was a simple matter of adding this setter to my style. This worked great! As you can see in the screen shot at the beginning of the post, I set the text appearance to the white, 18 size bold text I wanted. As you can seen in the screen shot, the commands are in a larger, bold font style.
This worked fine — mostly. If you look, you will see that each of the labels has the foreground set to a grey color. Here is the source code download.Represents the control that displays a header that has a collapsible window that displays content.
The following example shows how to create a simple Expander control. Expander is a HeaderedContentControlwhich means its Content and Header properties can be of any type such as string, image, or panel. For more information, see the HeaderedContentControl class.
The following illustration shows an Expander control. If the content of the expanded window is too large for the window, you can wrap the content of the Expander in a ScrollViewer control to provide scrollable content. Scrolling capability is not automatically provided by the Expander control. For an Expander to work correctly, do not specify a Height on the Expander control when the ExpandDirection property is set to Down or Up.
When you set a size on the Expander control in the direction that the expanded content is displayed, the area that is defined by the size parameter is displayed with a border around it. This area displays even when the window is collapsed. To set the size of the expanded window, set size dimensions on the content of the Expander control or the ScrollViewer that encloses the content. When an Expander control is the last element in a DockPanelthe Expander is sized to fill the remaining area of the DockPanel.
The alignment of Expander content can be defined by setting the HorizontalContentAlignment and VerticalContentAlignment properties on the control.
Expander Styles and Templates
These properties are applied to the header and to the contents of the expanded window. To apply the same property settings to multiple Expander controls, use the Style property. You can modify the default ControlTemplate to give the control a unique appearance. To see the parts and states that are specific to the Expandersee Expander Styles and Templates. Dependency properties for this control might be set by the control's default style. If a property is set by a default style, the property might change from its default value when the control appears in the application.
The default style is determined by which desktop theme is used when the application is running. Setting a visual property only has an effect if that property is both present in the control's default template and is set by using a TemplateBinding. Initializes a new instance of the Expander class. Identifies the Collapsed routed event.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
This topic describes the styles and templates for the xref:System. Expander control. You can modify the default xref:System. ControlTemplate to give the control a unique appearance. For more information, see Create a template for a control.
The following table lists the visual states for the xref:System. The following example shows how to define a xref:System. ControlTemplate for the xref:System.
For the complete sample, see Styling with ControlTemplates Sample. Skip to content. Permalink Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. Branch: master. Find file Copy path. Cannot retrieve contributors at this time. Raw Blame History. You signed in with another tab or window. Reload to refresh your session.
You signed out in another tab or window. The control uses the xref:System.This article will demonstrate how to create a ControlTemplate for the WPF Expander control to customize its appearance and behavior. First, a simplified version of the default template is explained in detail. Then, a couple more complex versions will be built on top of the default template with customizations such as adding animation and changing the look and feel.
The code shown in this article was designed to be both.
NET 3. NET 4 compatible. It is expected that the reader has basic understanding of basic WPF concepts such as Binding and Triggers. A basic knowledge of Animations will be very helpful. The reader should also be familiar or willing to lookup commonly used controls such as GridDockPanelToggleButtonetc.
Typically, an Expander control is visually composed of three parts. I'm going to call those parts as "icon", "header", and "content" for easier reference.
For example, "header" means the part in the diagram, while "Header" refers to some property in the XAML. Please excuse my MS Paint skills:. All of these parts are then laid out using a DockPanelbut it really could be any layout control of your choice. The first step is to create a ControlTemplate for the ToggleButton Expander 's button ; this will later be used within the Expander 's template. The code is shown below. I will also talk about a few key points after that.
We can "expose" properties of the component controls inside a template by binding them to the templated parent ToggleButton. For example, we can bind the Background property of the Border inside the template to the background of the templated parent the TargetType ToggleButton like in the above code. In this way, when we are using this template, if we set the ToggleButton 's Background property to some colour, the Border 's background will be set to that colour as well.
TemplateBinding is the more optimized version of standard binding as it is evaluated at compile time, but it does have some limitations. For example, it only supports the OneWay binding mode and does not allow you to set attributes such as Converters or StringFormat that standard Binding has. This method is more flexible but slower than TemplateBinding as it provides all the features that standard Binding has. Whatever we place inside the ToggleButton will be clickable for toggling the visibility of the Expander 's content.
If you do not want the user to toggle the Expander by clicking header components, then you can move the header parts e. Furthermore, for any Shape whose Fill property is null or unset will only be clickable on the " outline " of the Shape. This is by design. To work around this, one way is to wrap a Border with a set Background around all of the components of the template, but you must make sure that the Background property does not remain unset or null at runtime even if it is set using Binding.
WPF: Styling the Expander Control
Another way is to put a Rectangle with Fill set to Transparent on top of any Shape you are using which was done above. Note that this does not handle ExpandDirection and only includes a few basic TemplateBindings. For ExpandDirectionyou will need need to set Triggers to change the layout of the entire template according to ExpandDirection 's value. There are many things you can do to customize the template, I'm only going to list a couple of common examples here.
The Tag property is desgined to store custom information; its type is Object so you can mostly use it for anything you want. All FrameworkElement s and descendants inherit this property. For example, suppose you wanted separate background colours for the header and content parts of the Expander.In this article we will see how we can customize expander control in WPF. Let's add an Expander control. It would be difficult to customize the Expander in Visual Studioso let's fire up Expression Blend 3 and open the solution.
As you see in the above figure, we have opened the solution in Expression Blend 3 and we have started editing the template for Expander Control. Let's keep the styles in App. As you see in above figure, we have given a name for the style, that is PlusMinusExpanderStyle.
And we are saving the style in Application that is App. Now you must have the idea about the style after the given name as PlusMinus, yes you are right we will have the Plus sign when the Expander is not expanded and Minus sign when the expander is expanded. After adding the style, it would open for edit mode. Now if you see the above figure you can easily understand that we have the above elements that construct an Expander control. The "HeaderSite" is a ToggleButton.
The above image shows the default style resources associated with the Expander control. You can actually see four different styles, such as:. When the expander is expanded the arrow goes down, so at that time the style is ExpanderDownHeaderStyle, and the vice versa, that is, when the expander is not expanded the arrows goes up, and the style is ExpanderUpHeaderStyle.
So we got our two styles that we need to change. We don't have to create new style and do all the animation; we can use these existing styles and modify it. First we would select the ExpanderDownHeaderStyle and modify it. The above image displays the structure of the HeaderSite ToggleButton when customized.
Now zoom in to a bit larger view and modify the arrow, which is a Path element. As you see in above figure we have changed the Path, and given a Red solid color. The above figure displays the style is being checked when it is applied, and now we would customize this style. The structure of this ToggleButton is the same as the other but we have a different Path here, let's change this to something we want to do. As you see in above figure we have added one more path and changed the color of the Paths to Green.
And now it is looking like a Plus symbol. Now if you run the application with the styles implemented, you would see the expander is not expanding to display the Minus symbol. That is because the Setter property is not being set.