2012-06-26 93 views
0

我刚开始测试与WPF的水,我试图绑定扩展器的扩展属性和listview项目的选定属性,以便当列表视图项目选择了扩张或膨胀下去另一路试图在膨胀WPF绑定listview选择项目扩展器扩展

到目前为止,我已经得到了

<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> 

的扩大选择设置列表视图项,但我想不出如何引用绑定中的扩展器。任何帮助或在正确的方向微调将不胜感激。

感谢

回答

3

嗯..

你不能用自己的模板连接ListBoxItem的......因为基本上他们不知道......这不会在这里工作:

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

您也可以不火的膨胀器的触发,因为制定者不接受绑定..

<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> 

答案是这样的:

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

如果你愿意,你可以使用绑定,模式= OneWayToSource,根据您的需要..

+0

谢谢,我想通过拉从不同的例子部分我还挺有偏离轨道 - 这正是我想要做的。 –

+0

Hi @J Lennon,如果我想仅在展开时选择ListBoxItem而在折叠时不取消选择它,该怎么办? – Shankar

+0

我相信最好的办法是为ListBoxItem创建一个新的模板,但我建议你在这里创建一个自己的问题。 –