1

I've just started testing the water with WPF and I'm trying to bind the expanded property of an expander and the selected property of a listview item together so that when a list view item is selected the expander expands or going down another road trying to set the listview item to selected on expand of the expander

so far I've got

<ListView HorizontalAlignment="Stretch"  Name="listView1" VerticalAlignment="Stretch" SelectionMode="Single" >
        <ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="IsSelected" Value="{Binding Path=IsExpanded, Mode=TwoWay}"/>
            </Style>
        </ListView.Resources>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Expander>
                        <TextBlock Text="{Binding Name}"></TextBlock>
                </Expander>
            </DataTemplate>
        </ListView.ItemTemplate>            
    </ListView>

But I can't figure out how to reference the expander in the binding. Any help or a nudge in the right direction would be appreciated.

Thanks

Brett Smith
  • 2,992
  • 1
  • 20
  • 22

1 Answers1

5

Well..

You can not connect listboxitem with its own template ... Because basically they do not know... That would not work here:

 <Style TargetType="ListBoxItem">
             <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=expanderHeader,Mode=OneWay}" Value="True">
                 <Setter Property="IsSelected" value="True"/>
                    </ DataTrigger>
             </ Style.Triggers>
     </ Style>

You also can not fire a trigger of the expander, because setters do not accept binding ..   

<Expander.Style>
     <Style TargetType="Expander">
         <Style.Triggers>
             <Trigger Property="IsExpanded" Value="True">
                 <Setter Property="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}, Path=IsSelected}" Value="True"/>
             </ Trigger>
          </ Style.Triggers>
     </ Style>
</ Expander.Style>

The answer is this:

    <ListBox.ItemTemplate>
        <DataTemplate>
               <Expander  x:Name="expanderHeader" IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}}">
                 <!-- Content -->
                </Expander>
        </DataTemplate>
    </ListBox.ItemTemplate>

If you prefer you can use the binding, mode = OneWayToSource, depending on your needs ..

J. Lennon
  • 3,311
  • 4
  • 33
  • 64
  • 1
    Thanks I guess by pulling parts from different examples I kinda got off track - this is exactly what I was trying to do. – Brett Smith Jun 26 '12 at 02:38
  • 1
    Hi @J Lennon, what if I want to select the ListBoxItem only when Expanded and not deselect it when collapsed? – Shankar Raju Nov 01 '12 at 03:52
  • 1
    I believe the best way to do, is create a new template for ListBoxItem, but I suggest you to create an own question here. – J. Lennon Nov 01 '12 at 10:13