【问题标题】:Binding Combo Box to an array of string (WPF)将组合框绑定到字符串数组(WPF)
【发布时间】:2020-08-05 10:03:50
【问题描述】:

我正在尝试将组合框绑定到后面代码中的数组,如下所示:

private readonly string[] Bindlist ={..}
.
.
.
ComboBox newCombo = new ComboBox();
newCombo.ItemsSource = Bindlist ;

但是会抛出一个错误,从字符串到组合框的转换是不可能的。

知道如何解决这个问题吗?

谢谢

【问题讨论】:

  • 这能回答你的问题吗? WPF ComboBox binding ItemsSource
  • 这使用 XAML。我需要一个纯粹来自代码方面的解决方案。
  • Sure.
  • 没有人读过异常信息 :(
  • @vasily.sib 我愿意。然而,有时它是徒劳的xD

标签: c# wpf combobox


【解决方案1】:

我不知道你想做什么,但没有问题;

public partial class MainWindow : Window
{

    private readonly string[] Bindlist = { "str1", "str2" };

    public MainWindow()
    {
        InitializeComponent();

        ComboBox box = new ComboBox();
        box.ItemsSource = Bindlist;

    }
}

【讨论】:

  • 感谢您的回答,我会再试一次,看看为什么会出现错误。
猜你喜欢
  • 1970-01-01
  • 2013-12-19
  • 2017-07-07
  • 2012-05-05
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多