2014-02-27 24 views
0

我有一个ListBox组合框作为ListItems。每个ListItem中的组合框都是使用ListBox的ItemTemplate创建的。如何在xaml中编写ParentListBox.SelectedIndex?

现在,假设我有5个ListItems。即5个组合框。

For the 1st ListItem i.e. 1st ComboBox I would like to have all the Items from database as ItemsSource. 
For the 2nd ListItem i.e. 1st ComboBox I would like to have all the Items from database as ItemsSource. 
For the 3rd ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource. 
For the 4th ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource. 
For the 5th ListItem i.e. 1st ComboBox I would like to have only the selected Items from above Comboboxes as ItemsSource. 

所以,我认为我必须为不同的ComboBox使用不同的DataSource。

而要做到这一点,我有以下起始码:

<ComboBox....> 
    <ComboBox.Style> 
     <Style> 
      <Style.Triggers> 
       <DataTrigger Binding="ParentListBox.SelectedIndex" Value="0"> 
        <Setter Property="DataSource" Value="{Binding Path=ListCorrespondingToValue1}"/> 
       </DataTrigger> 
       <DataTrigger Binding="ParentListBox.SelectedIndex" Value="Non-0"> 
        <Setter Property="DataSource" Value="{Binding Path=ListCorrespondingToValue2}"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    <ComboBox.Style> 
</ComboBox> 

现在,我的问题是,我应该怎么用的ParentListBox.SelectedIndex INSEAD在上面的代码,我应该代之以非0?

+0

我不知道你是否可以从这里到达那里。为什么不把第一个盒子上的选定项目绑定到ViewModel中的一个属性,并在选定项目的属性设置器中使用适当的项目源加载下一个盒子? – safetyOtter

回答

2

你不使用的非零部分DataTrigger ......你只是把它作为一个StyleSetter,使成为默认值,然后DataTrigger改变DataSource财产只有当值是0。试试这个:

<ComboBox....> 
    <ComboBox.Style> 
     <Style> 
      <Setter Property="DataSource" Value="{Binding 
       Path=ListCorrespondingToValue2}"/> 
      <Style.Triggers> 
       <DataTrigger Binding="ParentListBox.SelectedIndex" Value="0"> 
        <Setter Property="DataSource" Value="{Binding 
         Path=ListCorrespondingToValue1}"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    <ComboBox.Style> 
</ComboBox> 
+0

谢谢你的回答。我从来不知道那个伎俩。 – Vishal

+1

这不是一个窍门......它只是在'Style.Setter'中设置一个属性,但我很高兴它有帮助。 – Sheridan