在MVVM框架的组合框中选择一个项目

本文关键字:项目 一个 选择 框架 MVVM 组合 | 更新日期: 2023-09-27 18:04:13

我将WPF组合框绑定到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}" />

和DataGrid绑定到ObservableCollection ItemTypeList和ItemType有一个嵌套的对象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);

是否在集合中没有找到方法,就像在列表中一样,我可以使用?

在MVVM框架的组合框中选择一个项目

SelectedItem而不是SelectedIndex上使用绑定

相关文章: