2014-02-27 26 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