Yesterday, I spent almost an hour on a small but annoying problem. I was working on a visual editor for an IDE, I develop for internal-use. Have I mentioned before that I started working (for more than a couple of months now) in a company again? Clearly, I haven’t been blogging actively passed year. Let’s see whether I will be able to change this in 2009 or not.
Anyway, this visual editor is for a DSL, or should I say a XAML grammar? It was the perfect opportunity to show the power of WPF. However;
Problem:
Visual editor’s job is to display the workflow in a tree view fashion. I chose displaying listboxes inside listboxes instead of a treeview. Each last node has an expander inside the listboxitem container. When user expands an item, containing listbox grows to display all of the items. I have no problem with that, in fact, that is exactly what I am grateful for. There is one catch, though. Expander controls not only expand but also shrink. When it does, listbox sits back and enjoys the void. A small sample to play with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<StackPanel> <ListBox x:Name='xNoFlower' Background='Fuchsia' ScrollViewer.CanContentScroll='{Binding ElementName=xAgreed, Path=IsChecked}'> <ListBoxItem Background='White'> <Expander Header='Click me'> Again please </Expander> </ListBoxItem> </ListBox> <TextBlock Text='Check if you object' /> <CheckBox x:Name='xAgreed' IsChecked='True'> On a flower, every color is nice. </CheckBox> </StackPanel> |
Solution:
You probably did know this. I did also, however when things are under multiple levels of ControlTemplates, DataTemplates and Styles, I was reluctant to go deep and investigate. Actually, I was reluctant to do anything at all. I had a great new years eve party and a greater headache. Luckily, WPF also provides an easy solution. Although, I spent a lot time to guess the problem.
1 2 3 4 |
<Style TargetType='{x:Type ListBox}'> <Setter Property='ScrollViewer.CanContentScroll' Value='False' /> </Style> |
Am I wrong to think that this was the default behavior when a control with a scrollviewer is not bounded? If I am, I can easily blame it on countless shots or hitting the floor so many times. Who else did work Friday, just to see “s?he” could not?
sadece title türkçe gerisi ingilizce garip yaklaşım
Thank you! I arrived to your blog for WPFix, but this gem just solved another of my my problems :)).
Solved my problem with DataGrid inside an expander not recovering it’s space!
Thanks!