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

发布于 2024-09-06 13:41:06 字数 994 浏览 1 评论 0原文

我有两个组合框,一个用于组织,一个用于区域。选择组织时,我希望区域组合框能够使用相关区域进行自身更新。选择组织和区域后,我可以在文本框中输入站点并将其存储到数据库(添加模式)。我已经使用以下代码完成了该操作:

<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。

现在,当我想编辑收藏中的网站时,我遇到了一个新问题。在编辑模式下,我希望预先选择并禁用两个下拉菜单(业务规则是我可以编辑站点名称,而不是它连接到的组织和区域)。因此,通过在“组织”组合框上设置 SelectedIndex 属性,我可以选择我的组织,但在“区域”组合框上执行相同操作时,它会失败并出现对象引用错误。 知道我做错了什么吗?

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 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>

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2024-09-13 13:41:06

您将需要更深入地检查您的代码。我完成了下面的快速示例,即使在禁用组合的情况下,它也可以很好地设置 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>

<代码>

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文