本文介绍了WPF,Access组合框选择项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个组合框,有四个静态可能的选项,如下所示
I have a combobox that has four static possible selections, shown below
MainWindow.xaml
<Grid> <ComboBox Name="combobox" IsEditable="True" SelectionChanged="ComboBox_SelectionChanged" Text="Default Text" <ComboBoxItem Name="ComboBoxItem1">1</ComboBoxItem> <ComboBoxItem Name="ComboBoxItem2">2</ComboBoxItem> <ComboBoxItem Name="ComboBoxItem3">3</ComboBoxItem> <ComboBoxItem Name="ComboBoxItem4">4</ComboBoxItem> </ComboBox> <StackPanel Name="dock"> </StackPanel> </Grid>我为使用此方法选择的每个组合框项目创建一个扩展器。
I'm creating an expander for each combobox item that is selected using this method.
MainWindow.xaml.cs
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var itemIndex = combobox.SelectedItem; Expander expander = new Expander { Header = thisbobox.Name }; dock.Children.Add(expander); }我知道我可以访问combobox名称,但我不知道访问comboboxitem名称。我已尝试使用
I know I can access the combobox name, but I'm not sure how to access the comboboxitem name. I've tried using
Header = thisbobox.ComboBoxItem.Name和其他变种,但我没有成功。我只是试图使每个扩展器的头到comboboxitem的名称。
And other variants of that, but I've had no success. I'm just trying to make the header of each expander into the name of the comboboxitem. Any help is appreciated.
推荐答案private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var itemIndex = combobox.Text; string name = (combobox.SelectedItem as ComboBoxItem).Name; var obj = dock.FindName("Exp_Name"); if (obj == null) { Expander expander = new Expander(); expander.Header = name; expander.Name = "Exp_Name"; dock.Children.Add(expander); this.RegisterName(expander.Name, expander); } else { var element = obj as Expander; element.Header = name; } }
希望这有帮助。
更多推荐
WPF,Access组合框选择项
发布评论