我刚刚开始使用 WPF 表单而不是 Windows Forms 表单。在 Windows 窗体中我可以这样做:
ComboBox.SelectedValue.toString();
这会很好用。
如何在 WPF 中执行此操作?好像没有这个选项
与旧的 WF 形式相比,我发现了一种有点奇怪的做法:
ComboBoxItem typeItem = (ComboBoxItem)cboType.SelectedItem;
string value = typeItem.Content.ToString();
嗯..我找到了一个更简单的解决方案。
String s = comboBox1.Text;
这样我就可以得到字符串形式的选定值。
我的XAML如下:
<ComboBox Grid.Row="2" Grid.Column="1" Height="25" Width="200" SelectedIndex="0" Name="cmbDeviceDefinitionId">
<ComboBoxItem Content="United States" Name="US"></ComboBoxItem>
<ComboBoxItem Content="European Union" Name="EU"></ComboBoxItem>
<ComboBoxItem Content="Asia Pacific" Name="AP"></ComboBoxItem>
</ComboBox>
内容显示为文本和 WPF 组合框的名称。要获取所选项目的名称,我遵循以下代码行:
ComboBoxItem ComboItem = (ComboBoxItem)cmbDeviceDefinitionId.SelectedItem;
string name = ComboItem.Name;
获取 WPF 组合框的选定文本:
string name = cmbDeviceDefinitionId.SelectionBoxItem.ToString();
确保您已在 XAML 文件中设置 ComboBox 的名称:
<ComboBox Height="23" Name="comboBox" />
在代码中,您可以使用
SelectedItem
属性访问所选项目:
MessageBox.Show(comboBox.SelectedItem.ToString());
这取决于您绑定到 ComboBox 的内容。如果您绑定了一个名为 MyObject 的对象,并且有一个名为 Name 的属性,请执行以下操作:
MyObject mo = myListBox.SelectedItem as MyObject;
return mo.Name;
这些怎么样:
string yourstringname = (yourComboBox.SelectedItem as ComboBoxItem).Content.ToString();
作为 ComboBox
SelectionChanged
事件处理程序中的变体:
private void ComboBoxName_SelectionChanged(object send ...
{
string s = ComboBoxName.Items.GetItemAt(ComboBoxName.SelectedIndex).ToString();
}
我遇到了类似的问题,并尝试了该线程中建议的许多解决方案,但发现 SelectionChanged 事件在 ComboBox 项实际更新以显示新选择之前触发(即,它总是在发生变化)。
为了克服这个问题,我发现最好使用自动传递给事件处理程序的 e 参数,而不是尝试直接从组合框中加载值。
XAML:
<Window.Resources>
<x:Array x:Key="Combo" Type="sys:String">
<sys:String>Item 1</sys:String>
<sys:String>Item 2</sys:String>
</x:Array>
</Window.Resources>
<Grid>
<ComboBox Name="myCombo" ItemsSource="{StaticResource Combo}" SelectionChanged="ComboBox_SelectionChanged" />
<TextBlock Name="MyTextBlock"></TextBlock>
</Grid>
C#:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string chosenValue = e.AddedItems[0].ToString();
}
解决这个问题很简单。我所做的就是将“SelectedValuePath”添加到我的 XAML 代码中,并将其绑定到我想要与组合框一起返回的模型属性。
<ComboBox SelectedValuePath="_Department"
DisplayMemberPath="_Department"
Height="23"
HorizontalAlignment="Left"
ItemsSource="{Binding}"
Margin="-58,1,0,5"
Name="_DepartmentComboBox"
VerticalAlignment="Center"
Width="268"/>
这很大程度上取决于盒子的填充方式。如果通过将
DataTable
(或其他集合)附加到 ItemsSource
来完成,您可能会发现将 SelectionChanged
事件处理程序附加到 XAML 中的框,然后在代码隐藏中使用它很有用:
private void ComboBoxName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cbx = (ComboBox)sender;
string s = ((DataRowView)cbx.Items.GetItemAt(cbx.SelectedIndex)).Row.ItemArray[0].ToString();
}
我在这里看到了另外两个答案,其中有不同的部分 - 一个有
ComboBoxName.Items.GetItemAt(ComboBoxName.SelectedIndex).ToString();
,看起来很相似,但没有将盒子投射到 DataRowView
,这是我发现我需要做的事情,另一个:((DataRowView)comboBox1.SelectedItem).Row.ItemArray[0].ToString();
,使用 .SelectedItem
代替 .Items.GetItemAt(comboBox1.SelectedIndex)
。这可能有用,但我决定的实际上是我上面写的两者的组合,并且不记得为什么我避免.SelectedItem
,除了在这种情况下它一定对我不起作用。
如果您动态填充该框,或者直接在 XAML 中使用下拉列表中的
ComboBoxItem
项目,这是我使用的代码:
private void ComboBoxName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cbx = (ComboBox)sender;
string val = String.Empty;
if (cbx.SelectedValue == null)
val = cbx.SelectionBoxItem.ToString();
else
val = cboParser(cbx.SelectedValue.ToString());
}
你会看到我有
cboParser
,在那里。这是因为 SelectedValue
的输出如下所示:System.Windows.Controls.Control: Some Value
。至少在我的项目中是这样的。所以你必须从中解析你的Some Value
:
private static string cboParser(string controlString)
{
if (controlString.Contains(':'))
{
controlString = controlString.Split(':')[1].TrimStart(' ');
}
return controlString;
}
但这就是为什么此页面上有这么多答案的原因。这很大程度上取决于你如何填充盒子,以及如何从盒子中获取价值。答案可能在某种情况下是正确的,但在另一种情况下可能是错误的。
在WPF设计中创建一个ComboBox SelectionChanged事件并设置ItemsSource =“{Binding}”:
代码:
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string ob = ((DataRowView)comboBox1.SelectedItem).Row.ItemArray[0].ToString();
MessageBox.Show(ob);
}
private void usuarioBox_TextChanged(object sender, EventArgs e)
{
string textComboBox = usuarioBox.Text;
}
MsgBox(cmbCut.SelectedValue().ToString())
要在 C# 中获取组合框选定索引的值,请使用:
Combobox.SelectedValue
原理是一样的。
您可以使用 SelectedIndex 和 ComboBox.Items[SelectedIndex].ToString()。或者只是 ComboBox.SelectedItem 并将其转换为您需要的任何类型:)
其实你也可以按照下面的方式来做。
假设您的组合框名称是
comboBoxA
。那么它的值可以得到为:
string combo = comboBoxA.SelectedValue.ToString();
我认为它现在得到了支持,因为你的问题已经有五年了。
这样写:
String CmbTitle = (cmb.SelectedItem as ComboBoxItem).Content.ToString()
如果你想获取该值并验证它,你可以这样做
string index = ComboBoxDB.Text;
if (index.Equals(""))
{
MessageBox.Show("your message");
}
else
{
openFileDialog1.ShowDialog();
string file = openFileDialog1.FileName;
reader = new StreamReader(File.OpenRead(file));
}
// -----------------------------------------------------------------
private void onSelectionChanged(object sender,
SelectionChangedEventArgs e)
{
String result = ((ComboBox)sender).SelectedItem.ToString();
// do something with result
}
// -----------------------------------------------------------------
我发现这很有用。我把它留在这里以防万一有人需要它:
获取值:
(comboBox1.SelectedItem as dynamic).Value
获取文本:
(comboBox1.SelectedItem as dynamic).Text
<ComboBox x:Name="TestComboBox" SelectionChanged="TestComboBox_SelectionChanged" Padding="2">
<ComboBoxItem>Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
</ComboBox>
方法1
string content = (((sender as ComboBox).SelectedValue) as ComboBoxItem).Content.ToString();
方法2
string content = (string)((ComboBoxItem)((ComboBox)sender).SelectedValue).Content;
您可以通过属性
SelectedValue
提取值。像这样:
combo.SelectedValue.ToString();
您的解决方案有效,但仅限于一种类型的对象,如果您想对所有对象使用相同的代码,请执行以下操作:
string? value = "";
Type t = SelectedItem.GetType();
PropertyInfo[] props = t.GetProperties();
foreach(var prop in props)
{
if (prop. Name == DisplayMemberPath)
value = prop.GetValue(SelectedItem).ToString();
}
我使用这个代码,它对我有用:
DataRowView typeItem = (DataRowView)myComboBox.SelectedItem;
string value = typeItem.Row[0].ToString();
XAML:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="19,123,0,0" Name="comboBox1" VerticalAlignment="Top" Width="33" ItemsSource="{Binding}" AllowDrop="True" AlternationCount="1">
<ComboBoxItem Content="1" Name="ComboBoxItem1" />
<ComboBoxItem Content="2" Name="ComboBoxItem2" />
<ComboBoxItem Content="3" Name="ComboBoxItem3" />
</ComboBox>
C#:
if (ComboBoxItem1.IsSelected)
{
// Your code
}
else if (ComboBoxItem2.IsSelected)
{
// Your code
}
else if(ComboBoxItem3.IsSelected)
{
// Your code
}
它对我有用:
System.Data.DataRowView typeItem = (System.Data.DataRowView)ComboBoxName.SelectedItem;
string value = typeItem.DataView.ToTable("a").Rows[0][0].ToString();