如何将我从组合框填充的组合框选中项目变为字符串,以便我可以在我的帖子中使用它,因为string bolts = comboBox_Copy.Text;
或string bolts = comboBox.Copy.SelectedItem;
提供{{1} }}
null
XAML
private void boltPatterns()
{
{
try
{
string Url = URL_Domain + "resources/bolt-pattern";
Uri serviceUri = new Uri(Url);
using (WebClient webClient = new WebClient())
{
webClient.Encoding = Encoding.UTF8;
string api = webClient.DownloadString(serviceUri);
List<boltPatterns> values = JsonConvert.DeserializeObject<List<boltPatterns>>(api);
comboBox_Copy.ItemsSource= values;
}
}
答案 0 :(得分:2)
string bolts = comboBox.SelectedItem.ToString();
答案 1 :(得分:1)
您正在使用绑定(这是一件好事),因此您不需要(并且在MVVM中大多数情况下不应该)访问组合框本身。
如果您想使用MVVM,请在您的顶级组件DataContext
上设置以启用对代码隐藏属性的绑定:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
然后在代码隐藏类中创建一个SelectedBoltPattern
类型的属性boltPatterns
(这就是你在例子中拼写它的方式)。在XAML中采用SelectedItem绑定
SelectedItem="{Binding SelectedBoltPattern}"
请注意,这与属性名称匹配。
在代码隐藏中,您可以使用this.SelectedBoltPattern
访问当前选定的项目。
一旦习惯了绑定,您可能希望使用像Caliburn.Micro这样的简单MVVM框架来完成最简单的应用程序。那些使这些事情变得非常容易。
答案 2 :(得分:0)
string bolts = comboBox.Text.ToString();