How can I hide the selected item in a WPF combo box?

有些话、适合烂在心里 提交于 2020-01-13 19:41:06

问题


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

item2
 item1
 item2
 item3

this:

item2
 item1
 item3

How can this be done?


回答1:


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




回答2:


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.




回答3:


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

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


来源:https://stackoverflow.com/questions/517002/how-can-i-hide-the-selected-item-in-a-wpf-combo-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!