如何在具有元素绑定的ComboBox中设置SelectedIndex?

时间:2010-06-21 08:05:47

标签: data-binding mvvm combobox silverlight-4.0 selectedindex

我有两个ComboBox,一个用于组织,一个用于Region。选择组织时,我希望Region组合框能够与相关区域一起更新。选择组织和区域后,我可以在站点中键入文本框并将其存储到db(ADD模式)。我用这段代码完成了这个:

<ComboBox x:Name="cbOrganisation"
      Grid.Row="0"
      Grid.Column="1" 
      ItemsSource="{Binding OrganisationEntries}"
      SelectedItem="{Binding SelectedOrganisation, Mode=TwoWay}"
      SelectedIndex="{Binding SelectedOrganisationIndex}"
      DisplayMemberPath="Description">
</ComboBox>

<ComboBox x:Name="cbRegions"
    Grid.Row="1"
    Grid.Column="1" 
    ItemsSource="{Binding ElementName=cbOrganisation, Path=SelectedItem.Regions}"
    SelectedItem="{Binding SelectedRegion, Mode=TwoWay}"
    SelectedIndex="{Binding SelectedRegionIndex}"
    DisplayMemberPath="Description" >
</ComboBox>

所以,我使用元素到元素绑定,第二个组合框将第一个组合为ItemSource。

现在,当我想在我的收藏中编辑网站时,我遇到了一个新问题。在编辑模式下,我希望预先选择和禁用两个下拉菜单(BusinessRule是我可以编辑网站名称,而不是它所连接的组织区域)。因此,通过在Organization combobox上设置SelectedIndex属性,我选择了我的组织,但是当在Regions组合框中执行相同操作时,它会因Object Reference错误而失败。 我有什么问题吗?

1 个答案:

答案 0 :(得分:1)

您需要更深入地检查代码。我做了下面的快速示例,即使组合被禁用,它也能正常设置SelectedIndex。如果您尝试设置过高或过低的索引,则会得到ArgumentOutOfRangeException,而不是空引用。

也许你正在使用Linq查询来查找列表中的项目但是没有检查项目是否实际被找到?也许您正在尝试使用SelectedRegion属性的内容,并且它为null,因为没有选择任何内容?

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = new Data();

        cbOrganisation.IsEnabled = false;
        cbRegions.IsEnabled = false;

        cbOrganisation.SelectedIndex = 2;
        cbRegions.SelectedIndex = 3;
    }
}

public class Data
{
    public Data()
    {
        OrganisationEntries = new List<Organisation>();
        OrganisationEntries.AddRange(new[]  { 
                                                new Organisation(){  Description = "Acme Products"
                                                                    ,Regions=new List<Region>(){     new Region(){Code="NY", Description="New York"}
                                                                                                    ,new Region(){Code="FL", Description="Florida"}
                                                                                                }
                                                                   }
                                                ,new Organisation(){ Description = "Acme Investments"
                                                                    ,Regions=new List<Region>(){    new Region(){Code="NY",Description="New York"}
                                                                                                    ,new Region(){Code="TX", Description="Texas"}
                                                                                                }
                                                                    }
                                                ,new Organisation(){ Description = "Acme Inflatable Cows"
                                                                    ,Regions=new List<Region>(){     new Region(){Code="WY", Description="Wyoming"}
                                                                                                    ,new Region(){Code="WA",Description="Washington"}
                                                                                                    ,new Region(){Code="IO", Description="Iowa"}
                                                                                                    ,new Region(){Code="KY", Description="Kentucky"}
                                                                                                }
                                                                    }
                                            });
    }

    public List<Organisation> OrganisationEntries { get; set; }

    public Organisation SelectedOrganisation { get; set; }
    public int SelectedOrganisationIndex { get; set; }

    public Region SelectedRegion { get; set; }
    public int SelectedRegionIndex { get; set; }
}

public class Organisation
{
    public Organisation()
    {
        Regions = new List<Region>();
    }
    public string Description { get; set; }
    public List<Region> Regions { get; set; }
}

public class Region
{
    public string Code { get; set; }
    public string Description { get; set; }
}

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="150" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <ComboBox x:Name="cbOrganisation"
              Grid.Row="0"
              Grid.Column="1" 
              ItemsSource="{Binding OrganisationEntries}"
              SelectedItem="{Binding SelectedOrganisation, Mode=TwoWay}"
              SelectedIndex="{Binding SelectedOrganisationIndex}"
              DisplayMemberPath="Description"
              Height="50"
              />

    <ComboBox x:Name="cbRegions"
                Grid.Row="1"
                Grid.Column="1" 
                ItemsSource="{Binding ElementName=cbOrganisation, Path=SelectedItem.Regions}"
                SelectedItem="{Binding SelectedRegion, Mode=TwoWay}"
                SelectedIndex="{Binding SelectedRegionIndex}"
                DisplayMemberPath="Description" 
                Height="50"
              />


</Grid>

<Grid x:Name="LayoutRoot" Background="White"> <Grid.ColumnDefinitions> <ColumnDefinition Width="150" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <ComboBox x:Name="cbOrganisation" Grid.Row="0" Grid.Column="1" ItemsSource="{Binding OrganisationEntries}" SelectedItem="{Binding SelectedOrganisation, Mode=TwoWay}" SelectedIndex="{Binding SelectedOrganisationIndex}" DisplayMemberPath="Description" Height="50" /> <ComboBox x:Name="cbRegions" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding ElementName=cbOrganisation, Path=SelectedItem.Regions}" SelectedItem="{Binding SelectedRegion, Mode=TwoWay}" SelectedIndex="{Binding SelectedRegionIndex}" DisplayMemberPath="Description" Height="50" /> </Grid>