如何将组合框中的项目设置为选定状态

发布于 2024-09-02 17:26:37 字数 122 浏览 1 评论 0原文

似乎没有人找到一种方法来将组合框项目设置为使用 SelectedItem="Binding Property" 进行选择。

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

It seems nobody has yet found a way to set the comboboxitem as selected with a SelectedItem="Binding Property".

Is the solution to use a IsSelected Property in the ViewModel object within the combobox itemssource?

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

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

发布评论

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

评论(3

眉黛浅 2024-09-09 17:26:37

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

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

Our successful approach for binding a combobox is the following...

<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");
        }
    }
}
烟雨扶苏 2024-09-09 17:26:37

不知道为什么在没有看到代码的情况下无法将数据绑定到 ComboBox 上的 SelectedItem。下面向您展示了如何使用 CollectionView 来执行此操作,CollectionView 内置了组合框支持的当前项目管理。 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>();
    }
}

Not sure why you can't data bind to SelectedItem on a ComboBox without seeing your code. Below shows you how to do it using a CollectionView which has current item management built in which comboboxes supports. CollectionView has a CurrentItem get property you can use to get currently selected.

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>

Code behind:

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>();
    }
}
平安喜乐 2024-09-09 17:26:37

我发现在组合框源代码中,selectitem是通过使用列表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;
            }
        }
    }

每次通过代码设置 Selecteditem 时, 始终返回 -1null
x = itemsCollection.IndexOf(value);
它被报告为组合框代码中的错误(82115)

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

这对我来说很好。

what i found is that in combobox soure code, selecteditem is set by using list selectedindex
combobox using

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

this method always return -1 or null every time you set Selecteditem by code
x = itemsCollection.IndexOf(value);
its reported as bug (82115) in combobox code

so the working method is to use SelectedIndex directly and bind to it instead of SelectemItem property and if you want you can read only the item from binding to the SelectedItem property or obtain it in your code using ItemsSource itself.

This working for me fine.

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