开发者

Selecting an item in a combobox in an MVVM framework

I have WPF combobox bound to a ObservableCollection ItemCategoryList

 <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}" />

and DataGrid bound to a ObservableCollection ItemTypeList and ItemType has a nested object ItemCategory of type 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>

Now when I select a row in the data grid I want the combobox to select respective ItemCategory of that ItemType

    private ItemType selectedItemType;

    public ItemType SelectedIt开发者_StackOverflow中文版emType
    {
        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");
        }
    }

Edit:

Problem seems to be here:

SelectIndexItemCategory = ItemCategoryList.IndexOf(SelectedItemCategory);

Is there no find method in Collection like in list that I may use?


Use binding on SelectedItem instead of SelectedIndex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜