如何在组合框中设置选定的项目

时间:2010-05-18 18:53:33

标签: wpf mvvm combobox set selecteditem

似乎没有人找到使用SelectedItem =“Binding Property”设置组合框的方法。

解决方案是在组合框项目源中的ViewModel对象中使用IsSelected属性吗?

3 个答案:

答案 0 :(得分:15)

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

<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 :(得分:5)

不知道为什么在没有看到代码的情况下,无法将数据绑定到ComboBox上的SelectedItem。下面将向您展示如何使用CollectionView进行操作,该ViewView具有组合框支持的当前项目管理。 CollectionView有一个CurrentItem get属性,可用于获取当前选中的内容。

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>();
    }
}

答案 2 :(得分:0)

我发现在组合框源代码中,selecteditem 是使用列表 selectedindex 设置的 组合框使用

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

每次通过代码设置-1时,此方法总是返回nullSelecteditem x = itemsCollection.IndexOf(value); 其报告为组合框代码中的错误 (82115)

所以工作方法是直接使用 SelectedIndex 并绑定到它而不是 SelectemItem 属性,如果你愿意,你可以只读取绑定到 SelectedItem 属性的项目或获取它在您的代码中使用 ItemsSource 本身。

这对我来说很好。

相关问题