如何在下拉框中设置某个选项为已选择状态

12

目前看来还没有人找到一种使用SelectedItem="绑定属性"将ComboBoxItem设置为选择状态的方法。

解决方案是在ComboBox的ItemsSource中使用ViewModel对象内部的IsSelected属性吗?

3个回答

16

我们成功绑定下拉框的方法如下...

<ComboBox 
    ItemsSource="{Binding Path=AllItems}" 
    SelectedItem="{Binding Path=CurrentItem, Mode=TwoWay}" />
<TextBlock Text="{Binding Path=CurrentItem, Mode=TwoWay}" />

class public ItemListViewModel
{
    public ObservableCollection<Item> AllItems {get; set;}

    private Item _currentItem;
    public Item CurrentItem
    {
        get { return _currentItem; }
        set
        {
            if (_currentItem == value) return;
            _currentItem = value;
            RaisePropertyChanged("CurrentItem");
        }
    }
}

1
这很奇怪。我敢肯定之前我已经按照你建议的做了,因为我在一些博客上读到过...现在我再试一次,它居然成功了 :P同时,如果有人感兴趣,我也帮忙解决了这个问题 XD// 在 UI 控件中将新创建的 Schoolclass 设置为选定索引 .. SelectedSchoolclassIndex = (Schoolclasses.Count != 0) ? Schoolclasses.Count - 1 : 0; - msfanboy
1
刚遇到了这个问题。我有两个不同的集合,忘记使用等于运算符,所以当前项被从另一个集合中选择,而不是我从XAML绑定的那个集合。因此,实现equals解决了这个问题。但是从同一个集合中选择也解决了这个问题。 - Rasmus Christensen

8

不知道你的代码为什么不能将数据绑定到ComboBox的SelectedItem上。以下展示了如何使用CollectionView来实现这一点,因为该类具有内置的当前项管理功能,可以支持ComboBox。CollectionView具有一个CurrentItem属性,您可以使用它来获取当前选择的项。

XAML:

<Window x:Class="CBTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <ComboBox 
            ItemsSource="{Binding Path=Names}"
            IsSynchronizedWithCurrentItem="True">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <TextBlock Text="{Binding Path=Names.CurrentItem}" />
    </StackPanel>
</Window>

后台代码:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;

namespace CBTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            DataContext = new VM();
        }
    }

    public class VM
    {
        public VM()
        {
            _namesModel.Add("Bob");
            _namesModel.Add("Joe"); 
            _namesModel.Add("Sally"); 
            _namesModel.Add("Lucy");

            Names = new CollectionView(_namesModel);

            // Set currently selected item to Sally.

            Names.MoveCurrentTo("Sally");
        }

        public CollectionView Names { get; private set; }

        private List<string> _namesModel = new List<string>();
    }
}

“不确定为什么您无法绑定ComboBox上的SelectedItem,必须查看您的代码才能解决问题...” 在谷歌上搜索一下,这是一个非常普遍的问题。对于我的情况来说CollectionView是完全不必要的开销,此外我不能也不会删除我的ObservableCollection<T> ,因为我需要它来进行添加/删除,而CollectionView没有这个功能。 - msfanboy
CollectionView可以是ObservableCollection的视图,因此不需要启动任何内容。你说CollectionView是完全的开销是什么意思?你是指除了Current之外CollectionView提供的其他功能,比如过滤、分组和排序吗?我仍然不知道在combobox上绑定selecteditem的问题是什么。 - Wallstreet Programmer
我不需要一个CollectionView;如果我想要排序,那应该由控件来完成,在我的情况下,DataGrid具有此功能。对于不会通过点击列标题进行排序的列表视图,CollectionView还算可以接受。 - msfanboy

0
我发现的是,在组合框源代码中,选定项是通过使用列表选定索引来设置的。组合框使用。
public object SelectedItem {
        get {
            int index = SelectedIndex;
            return (index == -1) ? null : Items[index];
        }
        set {
            int x = -1;

            if (itemsCollection != null) {
                //bug (82115)
                if (value != null)
                    x = itemsCollection.IndexOf(value);
                else
                    SelectedIndex = -1;
            }

            if (x != -1) {
                SelectedIndex = x;
            }
        }
    }

每次您通过代码设置Selecteditem时,此方法始终返回-1nullx = itemsCollection.IndexOf(value); 它在组合框代码中报告为错误(82115)

因此,有效的方法是直接使用SelectedIndex并将其绑定,而不是使用SelectemItem属性。如果您想要从绑定到SelectedItem属性中仅读取项目,则可以这样做,或者在您的代码中使用ItemsSource本身获取它。

这对我来说很好用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接