简体   繁体   English

wpf组合框下拉菜单

[英]wpf combo box dropdownopned

I am have three combo box on my xaml and I first one is loaded on page load , rest of them will be loaded on click event :Now I have DropDownOpened event which should load the combo and keep it open for the user to select but its hitting the statment to assign item source but then steps out of it 我的xaml上有三个组合框,我的第一个组合框是在页面加载时加载的,其余的将在click事件上加载:现在,我有DropDownOpened事件,该事件应加载组合并保持打开状态,以便用户选择,但是点击声明以分配项目来源,但随后退出

<Window x:Class="test_combo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Name="cbo1" Margin="40,37,328,250"  SelectionChanged="OnComboBoxChanged" />
        <ComboBox Name="cbo2" Margin="40,145,328,142" DropDownOpened="cbo2_DropDownOpened" />
        <ComboBox Name="cbo3" Margin="40,91,328,196" />
    </Grid>
</Window>

C# code: C#代码:

public partial class MainWindow : Window
{
     private List<string> comboList = new List<string>();
     string[] defaultParam =  { City , State ,zip} 

 public MainWindow()
        {
            InitializeComponent();

            foreach(string s in defaultParam)
            {
                LoadCombo(s);
            }

        }


        public void LoadCombo(string name)
        {           
            comboList.Add(name);
            cbo1.ItemsSource = comboList;         
        }


        private void OnComboBoxChanged(object sender,SelectionChangedEventArgs e)
        {
            string itemSel = (sender as ComboBox).SelectedItem.ToString();
            comboList.Remove(itemSel);
            MessageBox.Show(itemSel);

        }

        void cbo2_DropDownOpened(object sender, EventArgs e)
        {
            cbo2.ItemsSource = comboList;
        }
    }

它应该保持打开状态,但是如果没有打开,您可以通过使用强制打开它

 cbo2.IsDropDownOpen = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM