uwp 组合框 ContainerfromIndex(0) 总是返回 null [英] uwp combobox ContainerfromIndex(0) always returns null

查看:43
本文介绍了uwp 组合框 ContainerfromIndex(0) 总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在没有选择任何项目时选择组合框的第一项,但它不适用于 ContainerFromIndex(0) 或 ContainerFromItem.

I want to pick the first item of combobox when there is no item selected on it, but it's not working with ContainerFromIndex(0) or ContainerFromItem.

我不知道为什么它不会像这样工作,因为如果在组合框中选择了一个项目,索引方法就可以正常工作,那么为什么在没有选择项目时它不起作用,因为项目仍然存在.我什至可以轻松地从代码中的项目列表中获取第一个项目.基本上我想要第一个ComboBoxItem".

I am not sure why it won't work like this because the index method works fine if an item is selected on the combobox, then why won't it work when item isn't selected because items are still there. I can easily get the first item from the item list in code even. Basically I want the first "ComboBoxItem".

  if (comboBox.SelectedIndex == -1)
        {
            //var allItems = comboBox.Items.ToList();
            //var cccc = comboBox.Items.Count;
            //var firstItem = allItems.First();
            var ci = comboBox.ContainerFromIndex(0) as ComboBoxItem;
            if (ci != null)
            {
                ci.StartBringIntoView();
            }
        }

更新

我也尝试使用 Task.Delay(100) bcz 以下代码,这些项目应该是延迟加载的,但仍然将 ci 设为 null,奇怪的是 comboBox.Items 显示所有项目,并且comboBox.ItemSource 显示项目的所有计数,但 ContainerFromItem 仍返回 null.

Update

I also tried following code with Task.Delay(100) bcz the items are supposed to be lazy loaded, but still getting ci as null, weird thing is comboBox.Items shows all items, and comboBox.ItemSource shows all the count for the items, but ContainerFromItem still returns null.

  await Task.Delay(100);
  var comboBox = (ComboBox) sender;
  if (comboBox.SelectedIndex == -1)
  {                
       var ci = comboBox.Items[0];
       var cc = comboBox.ContainerFromItem(ci) as ComboBoxItem;
       if (cc != null)
       {
            cc.StartBringIntoView();
       }
  }

推荐答案

uwp 组合框 ContainerfromIndex(0) 总是返回 null

uwp combobox ContainerfromIndex(0) always returns null

请勾选Combobox样式,listview部分放在popup控件中,表示listview会延迟加载.如果你不打开下拉菜单.列表视图部分不会呈现.您可以选择一项,然后调用 ContainerFromIndex 方法来验证这一点.如果您想为 Combobox 提供默认选择项,请为 SelectedIndex 提供如下所示的值.

Please check Combobox style, the listview part was placed in the popup control, it means that the listview will be lazy load. if you don't open the dropdown. the listview part will not render. you could pick one item then call ContainerFromIndex method to verify this. If you want give it the default select item for Combobox please give SelectedIndex value like the following.

<ComboBox
    x:Name="CBX"
    Width="200"
    Header="Colors"
    PlaceholderText="Pick a color"
    SelectedIndex="0"
    SelectionChanged="ColorComboBox_SelectionChanged">
    <x:String>Blue</x:String>
    <x:String>Green</x:String>
    <x:String>Red</x:String>
    <x:String>Yellow</x:String>
</ComboBox>

这篇关于uwp 组合框 ContainerfromIndex(0) 总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆