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:
<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.
<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?

Comments 3
sadece title türkçe gerisi ingilizce garip yaklaşım
Posted 12 Jun 2009 at 3:10 am ¶Thank you! I arrived to your blog for WPFix, but this gem just solved another of my my problems
).
Posted 17 Jun 2009 at 1:11 pm ¶Solved my problem with DataGrid inside an expander not recovering it’s space!
Thanks!
Posted 25 May 2010 at 11:29 pm ¶Post a Comment