如何从 WPF Combobox 中获取选定的文本?
How to get selected text from WPF Combobox?
我正在尝试从 WPF ComboBox 获取文本,当它 select 但无法获取任何文本时。以下是我尝试过的代码。如果有人请帮助我在 select 不同的内容时从 ComboBox 获取文本。请注意,ComboBox 将从 sql 服务器加载数据,该服务器运行良好!
我的XAML代码是:
<ComboBox x:Name="comboID" TabIndex="27" SelectedValuePath="Content" SelectionChanged="comboID_SelectionChanged" HorizontalAlignment="Left" Margin="694,396,0,0" VerticalAlignment="Top" Width="165"/>
以下是我尝试过的 C# 代码示例:
string str1 = comboID.SelectedItem.ToString();
string str2 = comboID.SelectedValue.ToString();
string str3 = comboID.Text;
ComboBoxItem cmb = comboID.SelectedItem as ComboBoxItem;
var myVal = sender as ComboBox;
string value = comboID.SelectedItem as string;
我创建了一个简单的代码来给你一个大概的概念。
我的Xaml
<Window x:Class="Whosebug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="350">
<StackPanel Orientation="Horizontal">
<Label Content="Select Item" x:Name="LblSelectNumber" MinWidth="50px" Margin="5"></Label>
<ComboBox x:Name="ComboId" DisplayMemberPath="Number"
SelectedValuePath="Content" SelectionChanged="ComboID_OnSelectionChanged" MinWidth="100"
HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" />
</StackPanel>
</Window>
我的代码隐藏
public partial class MainWindow : Window
{
private readonly List<MyItem> _items;
public MainWindow()
{
InitializeComponent();
_items = new List<MyItem>
{
new MyItem{Content = "Test1",Number = "One"},
new MyItem{Content = "Test2",Number = "Two"},
new MyItem{Content = "Test3",Number = "Three"}
};
ComboId.ItemsSource = _items;
}
private void ComboID_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(ComboId.SelectedValue.ToString());
}
}
public class MyItem
{
public string Content { get; set; }
public string Number { get; set; }
}
请注意DisplayMemberPath
属性在ComboBox
中的用法
一个简单的建议。请在创建应用程序时使用 MVVM 模式,因为它将帮助您在较长的 运行 中维护解决方案。使用 MVVM 模式
可以很容易地实现我上面所做的一切
使用此代码代替您的代码在组合框中添加项目:
SqlConnection conn = new SqlConnection("your connection");
SqlCommand cmnd = new SqlCommand();
SqlDataReader sdr = null;
conn.Open();
cmnd.Connection = conn;
String query = "Select ID from Seller ORDER BY ID";
cmnd.CommandText = query;
sdr = cmnd.ExecuteReader();
while (sdr.Read())
{
comboID.Items.Add(sdr.GetString(0));
}
现在您可以使用:
string str = comboExporterID.SelectedItem.ToString();
简单的代码。试试这个,应该工作
string s = comboID.text;
将标签 属性 添加到组合框项目:
<Comboboxitem Tag="This Value"/>
然后
GetValue=ComboBoxName.SelectedItem.Tag.ToString()
GetValue 将是 "This Value" 而不是 System.windows.combobox blah blah blah..
我正在尝试从 WPF ComboBox 获取文本,当它 select 但无法获取任何文本时。以下是我尝试过的代码。如果有人请帮助我在 select 不同的内容时从 ComboBox 获取文本。请注意,ComboBox 将从 sql 服务器加载数据,该服务器运行良好!
我的XAML代码是:
<ComboBox x:Name="comboID" TabIndex="27" SelectedValuePath="Content" SelectionChanged="comboID_SelectionChanged" HorizontalAlignment="Left" Margin="694,396,0,0" VerticalAlignment="Top" Width="165"/>
以下是我尝试过的 C# 代码示例:
string str1 = comboID.SelectedItem.ToString();
string str2 = comboID.SelectedValue.ToString();
string str3 = comboID.Text;
ComboBoxItem cmb = comboID.SelectedItem as ComboBoxItem;
var myVal = sender as ComboBox;
string value = comboID.SelectedItem as string;
我创建了一个简单的代码来给你一个大概的概念。
我的Xaml
<Window x:Class="Whosebug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="350">
<StackPanel Orientation="Horizontal">
<Label Content="Select Item" x:Name="LblSelectNumber" MinWidth="50px" Margin="5"></Label>
<ComboBox x:Name="ComboId" DisplayMemberPath="Number"
SelectedValuePath="Content" SelectionChanged="ComboID_OnSelectionChanged" MinWidth="100"
HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" />
</StackPanel>
</Window>
我的代码隐藏
public partial class MainWindow : Window
{
private readonly List<MyItem> _items;
public MainWindow()
{
InitializeComponent();
_items = new List<MyItem>
{
new MyItem{Content = "Test1",Number = "One"},
new MyItem{Content = "Test2",Number = "Two"},
new MyItem{Content = "Test3",Number = "Three"}
};
ComboId.ItemsSource = _items;
}
private void ComboID_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(ComboId.SelectedValue.ToString());
}
}
public class MyItem
{
public string Content { get; set; }
public string Number { get; set; }
}
请注意DisplayMemberPath
属性在ComboBox
一个简单的建议。请在创建应用程序时使用 MVVM 模式,因为它将帮助您在较长的 运行 中维护解决方案。使用 MVVM 模式
可以很容易地实现我上面所做的一切使用此代码代替您的代码在组合框中添加项目:
SqlConnection conn = new SqlConnection("your connection");
SqlCommand cmnd = new SqlCommand();
SqlDataReader sdr = null;
conn.Open();
cmnd.Connection = conn;
String query = "Select ID from Seller ORDER BY ID";
cmnd.CommandText = query;
sdr = cmnd.ExecuteReader();
while (sdr.Read())
{
comboID.Items.Add(sdr.GetString(0));
}
现在您可以使用:
string str = comboExporterID.SelectedItem.ToString();
简单的代码。试试这个,应该工作
string s = comboID.text;
将标签 属性 添加到组合框项目:
<Comboboxitem Tag="This Value"/>
然后
GetValue=ComboBoxName.SelectedItem.Tag.ToString()
GetValue 将是 "This Value" 而不是 System.windows.combobox blah blah blah..