【问题标题】:WPF Combobox current scroll position does not changeWPF Combobox当前滚动位置不会改变
【发布时间】:2010-08-31 09:55:43
【问题描述】:

我在代码隐藏文件中有一个 ObservableCollection<ClassName> 类型的公共属性,我已将它绑定到 Combobox 的 ItemsSource 属性。

<ComboBox Height="23" 
                  Margin="82,34,71,0" 
                  Name="comboBox1" 
                  VerticalAlignment="Top"
                  ItemsSource="{Binding Path=Collection}"
                  DisplayMemberPath="Name" />

在我在表单加载时填充此集合后,所有项目都会显示,我向下滚动到最后一个元素并选择它。

现在,我单击一个按钮,它将向集合中添加另一个项目,我想将光标设置到列表的开头。为此,我尝试了以下代码,

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Collection.Add(new TempObject() { Name = "new item" });
        comboBox1.SelectedIndex = -1;
    }

这样做不会将滚动条设置到列表的开头。我尝试清除列表并再次填充它,但它仍然不起作用。

请帮忙....

应用 BringIntoView 后:

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            Collection.Clear();
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });
            Collection.Add(new TempObject() { Name = "testItem" });

            comboBox1.SelectedIndex = -1;

            ComboBoxItem item = comboBox1.ItemContainerGenerator.ContainerFromIndex(0) 
                                                                       as ComboBoxItem;

            if (item != null) item.BringIntoView();
     }

这将始终为 ComboBoxItem 项返回 null。

【问题讨论】:

    标签: c# wpf combobox


    【解决方案1】:

    试试这个:

    comboBox1.Items[0].BringIntoView();
    

    【讨论】:

    • BringIntoView 方法适用于 FrameworkElement 类型的对象。我可以将它用于自定义类型的项目吗?
    【解决方案2】:

    “我想将光标设置为列表的开头”是指您要将组合框的选定项设置为第一项吗?然后将其设置为索引0,索引-1表示没有选择。

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
        Collection.Add(new TempObject() { Name = "new item" }); 
        comboBox1.SelectedIndex = 0; 
    } 
    

    在您发表评论后更新:由于您的组合框是数据绑定的,您可以使用 ItemContainerGenerator 来获取第一个项目。这仅在项目已被渲染时才有效,即下拉列表已打开。

    private void button1_Click(object sender, RoutedEventArgs e)  
    {  
       Collection.Add(new TempObject() { Name = "new item" });  
       comboBox1.SelectedIndex = -1;  
       ComboBoxItem item = comboBox1.ItemContainerGenerator.ContainerFromIndex(0) as ComboBoxItem;
       if (item != null) item.BringIntoView();
    }  
    

    另一种更简单的方法是只选择第一项,然后取消选择它。

    private void button1_Click(object sender, RoutedEventArgs e)  
    {  
        Collection.Add(new TempObject() { Name = "new item" });  
        comboBox1.SelectedIndex = 0;  
        comboBox1.SelectedIndex = -1;  
    }
    

    【讨论】:

    • 没有。我不想选择任何项目。所选项目应为空。只想将滚动位置设置为下拉列表的开头。
    • 非常感谢您迄今为止的回复。但是使用 ItemContainerGenerator 可以正常工作,直到未重新实例化绑定列表。例如,如果我重新实例化或清除并将项目再次添加到集合中,ContainerFromIdex 将返回 null。因此,BringIntoView 方法将不起作用。有什么解决方法吗?
    • 这真的是个问题吗?如果您删除所有项目,我希望滚动位置也会被重置,因此无需获取第一个项目来调用 BringIntoView。你说的恢复是什么意思?请在您的问题中添加代码以显示您遇到的问题。
    • 很抱歉没有发布代码。请参阅问题,在“应用 BringIntoView”部分后获取与 Collection.Clear() 问题相关的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2013-04-06
    相关资源
    最近更新 更多