views:

435

answers:

2

I have a listbox where the items are styled using a ResourceDictionary style which is then attached to the ItemContainerStyle property. This gives my ListBoxItems a BorderThickness of let's say 1.

Now I want to collapse the items individually, so I use Visibility.Collapsed but for some reason the border that the ItemContainerStyle has created does not vanish with the rest of the list box item. It is as if it has created a layer behind my item and this remains despite the item being collapsed.

How do I set the ListBoxItem's (or this extra layer's) BorderThickness to 0 at run-time?

Regards sk

A: 
foreach(ListBoxItem item in listBox1.Items){
   item.BorderThickness = new Thickness(0);
}

This is the answer but I wouldnt recommend because you cant undo the style to bring back what was original instead you should choose some different approach with databinding on the basis of certain states.

Akash Kava
I have tried this and it doesn't apply it. Reflecting the BorderThickness value shows {BorderThickness 0,0,0,0}, although it should be {BorderThickness 0,0,0,1}
_NT
this is totally not work. it will return datamodel instead of listboxitem
ariso
A: 

try using a custom triggers:

    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="Visibility" Value="Collapsed">
                <Setter Property="BorderThickness" Value="0,0,0,0"/>
            </Trigger>
            <Trigger Property="Visibility" Value="Visible">
                <Setter Property="BorderThickness" Value="0,0,0,1"/>
            </Trigger>
        </Style.Triggers>
    </Style>

Obviously change your border thickness values, but this should do the trick (or something very close to this)

Alastair Pitts