【问题标题】:WPF set ComboBox to -1 index after click event of ComboBoxItem issue在 ComboBoxItem 问题的单击事件后,WPF 将 ComboBox 设置为 -1 索引
【发布时间】:2018-10-02 08:10:20
【问题描述】:

我有一个包含我的类别的组合框。在这个组合框中,我还有一个名为 的项目,它有一个点击事件。 For now, lets forget about that it opens a new window or dialog window to add a new category ... now I want whenever the is selected the combo box selected index change to -1.

<ComboBox x:Name="testcombo" HorizontalAlignment="Left" Margin="268,213,0,0" VerticalAlignment="Top" Width="120" Background="#FFC58383" DisplayMemberPath="data" SelectedValuePath="id">

                <ComboBox.ItemContainerStyle>
                    <Style TargetType="ComboBoxItem">
                        <EventSetter Event="PreviewMouseLeftButtonUp" Handler="ComboBoxItem_PreviewMouseLeftButtonUp"/>
                    </Style>
                </ComboBox.ItemContainerStyle>
</ComboBox>

和c#

namespace WpfApp4
{
    public partial class MainWindow : Window
    {
        public class Modell
        {
            public int id { get; set; }
            public string data { get; set; }
        }

        public MainWindow()
        {
            InitializeComponent();
            testcombo.Items.Add(new Modell { id = 0, data = "<--NEW-->" });
            testcombo.Items.Add(new Modell { id = 1 , data = "dddd" });
            testcombo.Items.Add(new Modell { id = 2, data = "dddzxcd" });
            testcombo.Items.Add(new Modell { id = 3, data = "ddczdd" });
        }



        private void ComboBoxItem_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var cat_obj = (sender as ComboBoxItem).Content as Modell;

            if (cat_obj.id == 0)
            {
                testcombo.SelectedIndex = -1;
                //MessageBox.Show("", "", MessageBoxButton.OK);
            }

        }
    }
}

问题是上面的代码没有将索引更改为-1,但是当我在testcombo.SelectedIndex = -1;它的行之后或之前添加一个消息框时:|

注意:我无法在组合框 SelectionChanged 中对 -1 进行索引,因为在主项目中我有 keyUp 事件,该事件通过键盘 arrowUp/Down 选择项目

【问题讨论】:

  • 从组合框中删除“新类别”项目并创建一个“新类别”按钮,在创建新类别后会将这个类别添加到组合框项目中。
  • 另一种在代码中设置索引的方法,您可以在其中为“新类别”打开一个窗口。

标签: c# .net wpf combobox


【解决方案1】:

使用Dispatcher 使其工作:

private void ComboBoxItem_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    var cat_obj = (sender as ComboBoxItem).Content as Modell;

    if (cat_obj.id == 0)
    {
        Dispatcher.BeginInvoke((Action)(() => { testCombo.SelectedIndex = -1; }));
        //MessageBox.Show("", "", MessageBoxButton.OK);
    }

}

另一种解决方案可能是:

private void ComboBoxItem_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    var cat_obj = (sender as ComboBoxItem).Content as Modell;

    if (cat_obj.id == 0)
    {
        testCombo.SelectedIndex = -1;
        e.Handled = true;
        testCombo.IsDropDownOpen = false;
    }
}

问题是,combobx 在MouseLeftButtonUp 发生后进行项目选择,因此覆盖SelectedIndex = -1;。使用 Dispatcher,您可以覆盖 SelectedIndex,它是通过鼠标单击设置的。使用第二种解决方案e.Handled = true; 组合框根本不选择该项目,但是您需要手动关闭下拉菜单。

【讨论】:

  • 谢谢@Rekshino ...这是否意味着该活动没有足够的时间来做到这一点(看起来像这样)或者什么?...因为它让我感到困惑
  • @RouzbehZarandi 欢迎您! :) 我已经更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 2011-09-01
相关资源
最近更新 更多