如何在WPF组合框中隐藏所选项?

时间:2020-12-15 15:05:49

I want to hide the selected item from the opened WPF combo box, basically to show instead of this:

我想隐藏打开的WPF组合框中的所选项目,基本上显示而不是显示:

item2
 item1
 item2
 item3

this:

item2
 item1
 item3

How can this be done?

如何才能做到这一点?

3 个解决方案

#1


Why don't you change the selected item's visibility instead?

为什么不改变所选项目的可见性呢?

#2


Found a hack for this, putting a label on top of the combo box:

找到了一个hack,在组合框的顶部放置一个标签:

Window1.xaml:

<Window x:Class="WpfApplication2.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">
    <Grid>
        <ComboBox Height="23" Margin="53,42,105,0" Name="comboBox1" VerticalAlignment="Top"
                  SelectionChanged="comboBox1_SelectionChanged" DropDownOpened="comboBox1_DropDownOpened"
                  DropDownClosed="comboBox1_DropDownClosed" GotFocus="comboBox1_GotFocus"
                  LostFocus="comboBox1_LostFocus"/>
        <Label Height="23" Margin="53,42,105,0" Name="label1" VerticalAlignment="Top" IsHitTestVisible="False">
            almafa
        </Label>
        <Button Height="23" Margin="89,0,114,108" Name="button1" VerticalAlignment="Bottom">Button</Button>
    </Grid>
</Window>

Window1.xaml.cs:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication2
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            comboBox1.Items.Add("alma");
            comboBox1.Items.Add("korte");
            comboBox1.Items.Add("szilva");
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox1.SelectedItem != null)
                comboBox1.SelectedItem = null;
        }

        private void comboBox1_DropDownOpened(object sender, EventArgs e)
        {
            label1.Foreground = Brushes.Black;
        }

        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            label1.Foreground = Brushes.White;
        }

        private void comboBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            if (!comboBox1.IsDropDownOpen)
                label1.Foreground = Brushes.White;
        }

        private void comboBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            label1.Foreground = Brushes.Black;
        }
    }
}

#3


Since the combobox's item's view is automatically generated from the collection of items it contains,
what you need to do is either remove the selected item from the combobox's items and set IsEditable="True" so that the selection will be valid.
You can place a label above the combobox which contains the selection to prevent the user from typing within the combobox.

Another solution would to be use 2 combobox, one with all the items and one with all the items but the item selected in the first combobox.
Then prevent the first combobox from expanding and place it above the second combobox.

由于组合框的项目视图是从它包含的项目集合中自动生成的,因此您需要做的是从组合框的项目中删除所选项目并设置IsEditable =“True”以使选择有效。您可以在组合框上方放置一个标签,其中包含选项,以防止用户在组合框中键入内容。另一个解决方案是使用2个组合框,一个包含所有项目,一个包含所有项目,但在第一个组合框中选择项目。然后防止第一个组合框扩展并将其放置在第二个组合框上方。

#1


Why don't you change the selected item's visibility instead?

为什么不改变所选项目的可见性呢?

#2


Found a hack for this, putting a label on top of the combo box:

找到了一个hack,在组合框的顶部放置一个标签:

Window1.xaml:

<Window x:Class="WpfApplication2.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">
    <Grid>
        <ComboBox Height="23" Margin="53,42,105,0" Name="comboBox1" VerticalAlignment="Top"
                  SelectionChanged="comboBox1_SelectionChanged" DropDownOpened="comboBox1_DropDownOpened"
                  DropDownClosed="comboBox1_DropDownClosed" GotFocus="comboBox1_GotFocus"
                  LostFocus="comboBox1_LostFocus"/>
        <Label Height="23" Margin="53,42,105,0" Name="label1" VerticalAlignment="Top" IsHitTestVisible="False">
            almafa
        </Label>
        <Button Height="23" Margin="89,0,114,108" Name="button1" VerticalAlignment="Bottom">Button</Button>
    </Grid>
</Window>

Window1.xaml.cs:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApplication2
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            comboBox1.Items.Add("alma");
            comboBox1.Items.Add("korte");
            comboBox1.Items.Add("szilva");
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox1.SelectedItem != null)
                comboBox1.SelectedItem = null;
        }

        private void comboBox1_DropDownOpened(object sender, EventArgs e)
        {
            label1.Foreground = Brushes.Black;
        }

        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            label1.Foreground = Brushes.White;
        }

        private void comboBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            if (!comboBox1.IsDropDownOpen)
                label1.Foreground = Brushes.White;
        }

        private void comboBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            label1.Foreground = Brushes.Black;
        }
    }
}

#3


Since the combobox's item's view is automatically generated from the collection of items it contains,
what you need to do is either remove the selected item from the combobox's items and set IsEditable="True" so that the selection will be valid.
You can place a label above the combobox which contains the selection to prevent the user from typing within the combobox.

Another solution would to be use 2 combobox, one with all the items and one with all the items but the item selected in the first combobox.
Then prevent the first combobox from expanding and place it above the second combobox.

由于组合框的项目视图是从它包含的项目集合中自动生成的,因此您需要做的是从组合框的项目中删除所选项目并设置IsEditable =“True”以使选择有效。您可以在组合框上方放置一个标签,其中包含选项,以防止用户在组合框中键入内容。另一个解决方案是使用2个组合框,一个包含所有项目,一个包含所有项目,但在第一个组合框中选择项目。然后防止第一个组合框扩展并将其放置在第二个组合框上方。