2011-07-30 106 views
2

我有綁定到的ObservableCollection ItemCategoryList選擇項目中的組合框在MVVM框架

<ComboBox Grid.Column="1" Grid.Row="2" Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="comboBox1" VerticalAlignment="Stretch" Width="Auto" ItemsSource="{Binding Path= ItemCategoryList}" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedItemCategory,UpdateSourceTrigger=PropertyChanged}" SelectedIndex="{Binding Path=SelectIndexItemCategory}" /> 

和DataGrid綁定到的ObservableCollection ItemTypeList WPF組合框而itemtype具有類型的嵌套對象ItemCategory ItemCategory

<DataGrid AutoGenerateColumns="False" Grid.ColumnSpan="3" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Margin="10" 
       ItemsSource="{Binding ItemTypeList}" 
       SelectedItem="{Binding SelectedItemType}" 
       CanUserDeleteRows="False" 
       CanUserAddRows="False"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding Path=ItemTypeID}" Header="ItemTypeID" IsReadOnly="True" Visibility="Hidden" /> 
      <DataGridTextColumn Binding="{Binding Path=Name}" Header="Item Type Name" IsReadOnly="True" /> 
      <DataGridTextColumn Binding="{Binding Path=ItemCategory.Name}" Header="Item Category" IsReadOnly="True" /> 
     </DataGrid.Columns> 
    </DataGrid> 

現在當我在數據網格中選擇一行時,我希望組合框選擇該ItemType的相應ItemCategory

private ItemType selectedItemType; 

    public ItemType SelectedItemType 
    { 
     get { return selectedItemType; } 
     set { 
      selectedItemType = value; 
      RaisePropertyChanged("SelectedItemType"); 
      if (selectedItemType != null) 
      { 
       ItemTypeName = selectedItemType.Name; 
       SelectIndexItemCategory = ItemCategoryList.IndexOf(SelectedItemCategory); 
      } 


     } 
    } 

    private int selectIndexItemCategory; 

    public int SelectIndexItemCategory 
    { 
     get { return selectIndexItemCategory; } 
     set { selectIndexItemCategory = value; 
     RaisePropertyChanged("SelectIndexItemCategory"); 
     } 
    } 

編輯:

問題似乎是在這裏:

SelectIndexItemCategory = ItemCategoryList.IndexOf(SelectedItemCategory); 

有沒有在列表中收集沒有找到方法,這樣我可以使用?

+0

你實現了INotifyPropertyChanged接口嗎? – Tigran

+0

是的,每次SelectedItemType和SelectIndexItemCategory 更改屬性正在命中。問題在這裏SelectIndexItemCategory = ItemCategoryList.IndexOf(SelectedItemCategory); – thewayman

+6

如果使用SelectedItem綁定而不是索引會發生什麼? – Tigran

回答

2

SelectedItem上使用綁定而不是SelectedIndex