开发者

How can I set the SelectedIndex in a ComboBox having element binding?

I have two ComboBoxes, one for Organisation and one for Region. When selecting Organisation I want the Region combobox to update itself with the related regions. After selecting Organisation and Region I can type in a Site to a textbox and store it to db (ADD mode). I've completed that with this code:

<ComboBox x:Name="cbOrganisation"
      Grid.Row="0"
      Grid.Column="1" 
      ItemsSource="{Binding OrganisationEntries}"
      SelectedItem="{Binding SelectedOrganisation, Mode=TwoWay}"
      SelectedIndex="{Binding SelectedOrganisationI开发者_高级运维ndex}"
      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>

So, I'm using element to element binding, with the second combobox having the first one as ItemSource.

Now, I've got a new problem when I want to EDIT a site in my collection. In EDIT mode, I want the two dropdowns to be preselected and disabled (BusinessRule is that I can edit the sitename, not which organisation og region it's connected to). So by setting the SelectedIndex property on Organisation combobox I get my organisation selected, but when doing the same on the Regions combobox it fails with an Object Reference error. Any clue what I'm doing wrong?


You are going to need to check a bit deeper in to your code. I did up the quick sample below, and it works fine setting the SelectedIndex even when the combos are disabled. If you try and set an index that is too high or low then you get an ArgumentOutOfRangeException, not a null reference.

Maybe you are using a Linq query to find an item in the list but not checking if an item is actually found? Maybe you are trying to use the contents of the SelectedRegion property and it is null as nothing is selected?

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>

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜