combobox 相关问题

Combobox允许从多个选项中选择一个(类似于下拉列表),或者键入自定义选项。

在 ComboBox DataTemplate 中获取 TextBlock 的值

我有以下 XAML: 我有以下 XAML: <ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="6" Grid.Column="2" Name="cbo_team" VerticalAlignment="Top" Width="148" DataContext="{Binding ElementName=cbo_component, Path=SelectedItem}" SelectedIndex="0"> <ComboBox.ItemsSource> <Binding XPath="Teams/Team/@id" Converter="{StaticResource xmlConverter}"> <Binding.ConverterParameter> <local:XmlConverterParameter XPathTemplate="/Products/Teams/Team[{0}]" XPathCondition="@id='{0}'" /> </Binding.ConverterParameter> </Binding> </ComboBox.ItemsSource> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding XPath=@name}" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> 在 C# 中,我试图获取 TextBlock 中当前选定项中的 ComboBox 的值。我怎么做? 这个问题差不多,但唯一的答案没有帮助。 检查这个样本。文本块(组合框下方)显示组合框中当前选定的 xml 元素的名称属性的值。将弹出一个消息框,其中包含在可视化树中查找的相同结果。初始选择更改时查找失败。看起来组合框项目是在设置所选项目后创建的。 XAML: <Window x:Class="CBTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Window.Resources> <XmlDataProvider x:Key="UsersData" XPath="Users"> <x:XData> <Users xmlns=""> <User name="Sally" /> <User name="Lucy" /> <User name="Linus" /> <User name="Charlie" /> </Users> </x:XData> </XmlDataProvider> </Window.Resources> <StackPanel> <ComboBox Name="_comboBox" ItemsSource="{Binding Source={StaticResource UsersData},XPath=*}" SelectedIndex="0" SelectionChanged="OnComboBoxSelectionChanged"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding XPath=@name}" Name="nameTextBlock" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> <!-- Below shows how to get the value of selected item directly from the data. --> <TextBlock DataContext="{Binding Path=SelectedItem, ElementName=_comboBox}" Text="{Binding XPath=@name}" /> </StackPanel> </Window> 后面的代码,展示了如何通过遍历可视化树直接获取文本: private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBox comboBox = sender as ComboBox; ComboBoxItem comboBoxItem = comboBox.ItemContainerGenerator.ContainerFromItem(comboBox.SelectedItem) as ComboBoxItem; if (comboBoxItem == null) { return; } TextBlock textBlock = FindVisualChildByName<TextBlock>(comboBoxItem, "nameTextBlock"); MessageBox.Show(textBlock.Text); } private static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var child = VisualTreeHelper.GetChild(parent, i); string controlName = child.GetValue(NameProperty) as string; if (controlName == name) { return child as T; } T result = FindVisualChildByName<T>(child, name); if (result != null) return result; } return null; } 抱歉来晚了一点 :) 但以下也有效(具有讽刺意味的是和你在同一个修复中!!) TextBlock tb1 = (TextBlock)cbo_team.SelectedItem; MessageBox.Show(tb1.Text); private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox ContactListBox = sender as ListBox; ListBoxItem listBoxItem = ContactListBox .ItemContainerGenerator.ContainerFromItem(ContactListBox.SelectedItem) as ListBoxItem; if (listBoxItem == null) { return; } TextBlock txtBlock = FindVisualChildByName<TextBlock>(listBoxItem, "ListTextBlock"); MessageBox.Show(txtBlock.Text); } private static T FindVisualChildByName<T>(DependencyObject parent, string name) where T : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { var child = VisualTreeHelper.GetChild(parent, i); string controlName = child.GetValue(NameProperty) as string; if (controlName == name) { return child as T; } T result = FindVisualChildByName<T>(child, name); if (result != null) return result; } return null; } 其他人已经建议使用 SelectionChanged 事件。没有测试下面的代码,但你可以试一试。 private void OnMyComboBoxChanged(object sender, SelectionChangedEventArgs e) { TextBlock tvContent = (sender as ComboBox).SelectedItem as TextBlock; string content = tvContent.Text; } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var comboBox = (ComboBox)sender; ComboBoxItem comboAsTextblock = (ComboBoxItem)comboBox.SelectedItem; string comboBoxItemText = comboAsTextblock.Content.ToString(); // comboBoxItemText is what you want :) }

回答 5 投票 0

How to make combobox editable in a tablecell in javafx?

我已经使用 ComboBoxTableCell 在表格单元格中设置了一个组合框,现在我希望这个组合框可编辑,以便用户可以相应地对其进行编辑。我已经使组合框的可编辑属性为真,但是......

回答 2 投票 0

VBA选择下拉列表值

我已经为一家公司接管了一个电子表格,它根据输入进行各种计算,包括很多下拉(ComboBox)列表。 我正在尝试编写一个 VBA 脚本来自动选择一个...

回答 0 投票 0

如果值不正确,将焦点重新放在 ComboBox 上

我正在尝试设置我的组合框,以便用户可以从列表中选择或设置自己的值(组合框用于自定义分辨率,因此会有默认值,或者他们可以给...

回答 2 投票 0

当 Combobox Itemsource 绑定到 WPF 中的 List/ObservableCollection 时,如何在运行时更新/刷新 Combobox Items/内容?

我有一个使用 ComboBox 的 WPF 应用程序。我的 ComboBox ItemSource 绑定到我在后台从 C# 填充的列表 <>。 这是 List<> 的 C# 代码: 公开部分...

回答 1 投票 0

如何从 ComboBox 访问值?

我想知道如何访问 ComboBox 的值。我尝试了几种语法可能性,但无法在线找到解决方案。将来我需要两个 ComboBox 的值来向表中添加新行...

回答 1 投票 0

System.ArgumentException:“设置 DataSource 属性后无法修改项目集合。”

我正在 winform 应用程序中创建一个 sigup,当运行该程序时,我在 comboBox1 行中收到此异常错误。这是我的代码。 cmd.Parameters.AddWithValue("@

回答 0 投票 0

在组合框中获取空值

我需要帮助。我有一个效果很好的组合框表,看起来像这样: 私人工人 _currentWorkers = new Workers(); 公共工人编辑添加(工人选择工人) {

回答 0 投票 0

通过 AppendToMapping 在 MAUI.NET 中布局 Windows Picker

我必须改变标准选择器控件的外观。目前仅适用于 Windows。所以我做了 AppendToMapping 并成功更改了背景颜色。 MauiProgram.cs 中的代码:

回答 1 投票 0

将文本框中的文本输入组合框 vb.net

我有两种形式,A和B。 在 FORM 上,用户将从组合框中选择一个国家代码,然后将其保存到数据库中。 在 FORM B 上,一个文本框显示了早期保存到数据库中的国家/地区代码...

回答 5 投票 0

如何使用组合框(Tkinter)?

我目前正在从事一个使用 Tkinter 作为 GUI 框架的零售管理系统项目。该系统的界面设计为使用图像作为背景,按钮放置在...

回答 1 投票 0

取消选择 datagridview 单元格中的组合框值 vb.net

我有一个带有 2 列(Column1 和 Column2)的简单数据网格视图。 Column1 中的单元格有一个 TextBox,Column2 中的单元格有一个带有两种颜色选项(蓝色或红色)的 ComboBox 首先,我想改变...

回答 0 投票 0

Forms: ComboBox: How to set "null" value

我有一个从标准组合框派生的类,我在那里将项目设置为数据源 公共密封类 TagComboBox : ComboBox { public void SetItems(List 标签) { t...

回答 1 投票 0

组合框中的空行

从这里线程 它工作正常,当我插入我的 USB 到 TTL 适配器创建一个端口(comport10)时,它显示在框中,但是我想要一个初始的空组合框,用户...

回答 3 投票 0

在 Access 中使用下一步和后退命令按钮时,组合框不会更改

当我从组合框中选择一个值时,会出现正确的记录,但是当我使用导航按钮时,组合框中的值不会改变;然而,记录确实随着导航而改变

回答 1 投票 0

Windows 窗体中的 .Net framework 4.8 AccessibleObject 不会读取整个组合框条目

我们有一个可访问性程序 (jaws),它通过 AccesibleObject 读取我的 Windows 窗体应用程序。当用户导航到组合框(带有选项卡)时,所选项目的文本将...

回答 0 投票 0

Windows 窗体中的 .Net framework 4.8 AccessibleObject 不读取整个组合框条目

我们有一个可访问性程序 (jaws),它正在通过 AccesibleObject 读取我的 Windows 窗体应用程序。当用户导航到组合框(带选项卡)或在组合框中导航时 ...

回答 0 投票 0

更改组合框的值(headlessui/react)

我正在研究 Headlessui/React 的 Combobox 组件。我正在使用数组来存储选定的值 const App = () => { const [instNames, setInstNames] = useState(Array(6).fill("&quo...

回答 0 投票 0

Vaadin 组合框分隔符

我目前正在开发一个 Vaadin 应用程序,我已经需要一个 ComboBox,在项目之间有一些分隔符。我一直在四处寻找,目前似乎没有办法实现......

回答 2 投票 0

从 VBA 开发的用户窗体的数据输入有问题吗?

依赖组合框的数据输入困难。我想要一列中的类别组合框和相邻列中的子类别的数据条目。但是有可能有多个类别和电子...

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.