Silverlight 为什么可以';在数据绑定组合框上设置SelectedIndex吗?

Silverlight 为什么可以';在数据绑定组合框上设置SelectedIndex吗?,silverlight,data-binding,xaml,combobox,Silverlight,Data Binding,Xaml,Combobox,我有两个ComboBox元素,一个有数据绑定,一个没有 在没有的一个上,我可以将SelectedIndex设置为精细 但是在数据绑定的一个上,如果我设置SelectedIndex,它会说,“AG_E_INVALID_参数” 但如果我将其设置为ViewModel中的一个值(SelectedIndex=“{Binding SelectedCustomerIndex}”),则它会显示“对象引用未设置为对象的实例”” 有人知道为什么我不能在这样数据绑定的组合框上设置SelectedIndex吗? XAM

我有两个ComboBox元素,一个有数据绑定,一个没有

在没有的一个上,我可以将SelectedIndex设置为精细

但是在数据绑定的一个上,如果我设置SelectedIndex,它会说,“AG_E_INVALID_参数

但如果我将其设置为ViewModel中的一个值(SelectedIndex=“{Binding SelectedCustomerIndex}”),则它会显示“对象引用未设置为对象的实例”

有人知道为什么我不能在这样数据绑定的组合框上设置SelectedIndex吗?

XAML:

<Grid>
    <StackPanel>
        <Border CornerRadius="5" Background="#eee" >
            <StackPanel HorizontalAlignment="Left" VerticalAlignment="top" Width="250">
                <ComboBox ItemsSource="{Binding Customers}" SelectedIndex="0"
             ItemTemplate="{StaticResource DataTemplateCustomers}"/>
            </StackPanel>
        </Border>
        <ComboBox x:Name="WhichNumber" Width="100" HorizontalAlignment="Left" Margin="10" SelectedIndex="0">
            <ComboBoxItem Content="One"/>
            <ComboBoxItem Content="Two"/>
            <ComboBoxItem Content="Three"/>
        </ComboBox>
    </StackPanel>
</Grid>
using System.Collections.ObjectModel;
using System.ComponentModel;
using TestBasics737.Models;

namespace TestBasics737.ViewModels
{
    public class PageViewModel : INotifyPropertyChanged
    {

        public PageViewModel()
        {
            Customers.Add(new Customer { FirstName = "Jim", LastName = "Smith" });
            Customers.Add(new Customer { FirstName = "Angie", LastName = "Smithton" });

            SelectedCustomerIndex = 0;

        }



        #region ViewModelProperty: SelectedCustomerIndex
        private int _selectedCustomerIndex = 0;
        public int SelectedCustomerIndex
        {
            get
            {
                return _selectedCustomerIndex;
            }

            set
            {
                _selectedCustomerIndex = value;
                OnPropertyChanged("SelectedCustomerIndex");
            }
        }
        #endregion

        #region ViewModelProperty: Customers
        private ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();
        public ObservableCollection<Customer> Customers
        {
            get
            {
                return _customers;
            }

            set
            {
                _customers = value;
                OnPropertyChanged("Customers");
            }
        }
        #endregion


        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}

视图模型:

<Grid>
    <StackPanel>
        <Border CornerRadius="5" Background="#eee" >
            <StackPanel HorizontalAlignment="Left" VerticalAlignment="top" Width="250">
                <ComboBox ItemsSource="{Binding Customers}" SelectedIndex="0"
             ItemTemplate="{StaticResource DataTemplateCustomers}"/>
            </StackPanel>
        </Border>
        <ComboBox x:Name="WhichNumber" Width="100" HorizontalAlignment="Left" Margin="10" SelectedIndex="0">
            <ComboBoxItem Content="One"/>
            <ComboBoxItem Content="Two"/>
            <ComboBoxItem Content="Three"/>
        </ComboBox>
    </StackPanel>
</Grid>
using System.Collections.ObjectModel;
using System.ComponentModel;
using TestBasics737.Models;

namespace TestBasics737.ViewModels
{
    public class PageViewModel : INotifyPropertyChanged
    {

        public PageViewModel()
        {
            Customers.Add(new Customer { FirstName = "Jim", LastName = "Smith" });
            Customers.Add(new Customer { FirstName = "Angie", LastName = "Smithton" });

            SelectedCustomerIndex = 0;

        }



        #region ViewModelProperty: SelectedCustomerIndex
        private int _selectedCustomerIndex = 0;
        public int SelectedCustomerIndex
        {
            get
            {
                return _selectedCustomerIndex;
            }

            set
            {
                _selectedCustomerIndex = value;
                OnPropertyChanged("SelectedCustomerIndex");
            }
        }
        #endregion

        #region ViewModelProperty: Customers
        private ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();
        public ObservableCollection<Customer> Customers
        {
            get
            {
                return _customers;
            }

            set
            {
                _customers = value;
                OnPropertyChanged("Customers");
            }
        }
        #endregion


        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}
使用System.Collections.ObjectModel;
使用系统组件模型;
使用TestBasics737.模型;
命名空间TestBasics737.ViewModels
{
公共类PageViewModel:INotifyPropertyChanged
{
公共页面视图模型()
{
添加(新客户{FirstName=“Jim”,LastName=“Smith”});
添加(新客户{FirstName=“Angie”,LastName=“Smithton”});
SelectedCustomerIndex=0;
}
#区域视图模型属性:SelectedCustomerIndex
private int_selectedCustomerIndex=0;
公共int-SelectedCustomerIndex
{
得到
{
return\u selectedCustomerIndex;
}
设置
{
_selectedCustomerIndex=值;
OnPropertyChanged(“SelectedCustomerIndex”);
}
}
#端区
#区域ViewModelProperty:客户
私有ObservableCollection_客户=新ObservableCollection();
公开收集客户
{
得到
{
退回客户;
}
设置
{
_顾客=价值;
不动产变更(“客户”);
}
}
#端区
#区域属性更改块
公共事件属性更改事件处理程序属性更改;
受保护的无效OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
#端区
}
}

SelectedItem确实喜欢这样