c# - WPF 组合框与 List<string> 绑定(bind)

标签 c# wpf

我有两个属性,一个是字符串列表,另一个只是一个字符串。

private List<String> _property;
public List<String> Property
get
{
return new List<string>(){"string1", "string2"};
}
set{_property = value
}

public String SimpleStringProperty{get;set;}

我还有一个在 XAML 中定义的 Combobox

<Combobox ItemsSource="{Binding Property , Mode="TwoWay"}" Text="Select Option" />    

现在组合框正确显示两个选项:“string1”和“string2”

当用户选择一个或另一个时,我想设置 SimpleStringProperty具有该值。但是,通过双向绑定(bind)从组合框返回的“值”不是 selectedItem,而是 List<String>。 .我该怎么做呢?我是 wpf 的新手,所以请原谅我的业余行为。

最佳答案

<Combobox ItemsSource="{Binding Property}" SelectedItem="{Binding SimpleStringProperty, Mode=TwoWay}" Text="Select Option" />

那是未经测试的,但它至少应该非常接近您的需要。

关于c# - WPF 组合框与 List<string> 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21898022/

相关文章:

c# - c#、java中的null是什么?

c# - "Global"事件处理器

c# - DataGrid 嵌套在 DataGrid 问题中

WPF TextBlock 在多行上显示字符串

wpf - TreeView 为所选项目显示蓝色

c# - 调试器没有跳过/进入异步/等待

c# - 如何通过rs485串口发送和接收?

c# - Caliburn Micro - 从模型类的 ItemSource 绑定(bind)自动创建 ViewModel 对象?

c# - 将项目添加到 ListView 时,MultiDataTrigger 的 EnterActions 中的 ColorAnimation 不触发

c# - 获取值存储过程 C#