WPF Troubleshooting
During my time as a consultant I noticed, that there were typical traps in WPF, where developers can loose a lot of time. So I decided to make a hitlist of the most common mistakes and solutions how to resolve them. I hope this helps.
Layout
- Scrollbar is not active or visible
- If your control is within a vertical stackpanel, it gives the control infinite height to layout. Consider replacing the stackpanel by a dockpanel.
- I created a data template and set HorizontalAlignment to Stretch but the item is not stretched
- Set the
HorizontalContentAlignment
on the list to Stretch
DataBinding
- I changed a value, but the binding is not reflecting my changes
- Check the output window in VisualStudio, if there are any binding errors.
- Does your data support INotifyPropertyChanged?
- Just firing a PropertyChanged event without changing the data does not work. Because the binding checks if oldvalue!=newvalue
Performance
- My list of items takes too long to render
- Your list is not virtualized. This means, all items will be generated, even if they are not visible. To avoid this check the following points:
ScrollViewer.CanContentScrol
must be set to False
- Grouping must be disabled
- You replaced the ItemsPanel by one that does not support virtualization
- You are using a too complex data template.
- Animations cause a high CPU load.
- WPF cannot use hardware acceleration and does software rendering. This can be because of the following points:
- You have set
AllowTransparency
to True
on your window.
- You are using legacy
BitmapEffects
instead of fast pixel shaders (Effects
).
- Your graphics adapter or driver does not support DirectX
Custom Controls
- I created a custom control, but the template it not showing
- Check if you have overriden the metadata of the
DefaultStyleKeyProperty
and set it to your type.
- Check if your template is surrounded by a style and both have the right
TargetType
- Check if the resource dictionary that contains the default style is loaded
- I use {TemplateBinding} in my ControlTemplate, but is not working
- In most cases you have to replace
{TemplateBinding Property}
by {Binding Property RelativeSource={RelativeSource TemplatedParent}}
- You can only use TemplateBinding within the content of your control template. It will not work anywhere else!
- If you want to access a parent property in the trigger, you have to use a normal binding, with relative source Self.
- TemplateBinding works only within the VisualTree of the template. You cannot use it on items that are only in the logical tree. Neighter on Freezables or to do two-way binding.
Last modified: 2011-03-23 07:02:53
Copyright (c) by Christian Moser, 2011.
Comments on this article
|
Amit |
|
|
Commented on 13.December 2009 |
Awesome! Thanks a lot |
|
|
|
Harendra kumar |
|
|
Commented on 4.January 2010 |
Awesome! thanks for valuable article. |
|
|
|
AbDoU-VB |
|
|
Commented on 5.March 2010 |
Hi , Thanks .. bat give me a picture of resulta |
|
|
|
James |
|
|
Commented on 26.May 2010 |
Addressed the features in the simplest way - Great |
|
|
|
Tom |
|
|
Commented on 8.July 2010 |
This is great stuff.. i will take a print out of this. |
|
|
|
Helix |
|
|
Commented on 19.July 2010 |
Good |
|
|
|
vinoth kumar |
|
|
Commented on 29.September 2010 |
how to work with wpf |
|
|
|
Sushil |
|
|
Commented on 28.December 2010 |
Give more senario |
|
|
|
Qutub |
|
|
Commented on 18.March 2011 |
Great work...! |
|
|
|
Qutub |
|
|
Commented on 18.March 2011 |
Great work...! |
|
|
|
Qutub |
|
|
Commented on 18.March 2011 |
Great work...! |
|
|
|
subramani |
|
|
Commented on 11.May 2011 |
Great work.! |
|
|
|
sterennlt |
|
|
Commented on 13.May 2011 |
THX!!!! u saved me HOURS! |
|
|
|
Dimas |
|
|
Commented on 17.August 2011 |
Could u pls put more info in? |
|