我使用以下代码显示组合框中的项目。但是项目没有显示出来。
代码:
<ComboBox Width="100" ItemsSource="{Binding}" SelectedIndex="0" Name="cbProduct"/>
List<ComboObject> combObjList = new List<ComboObject>();
combObjList.Add(new ComboObject { Text = "All", Value = "%" });
combObjList.Add(new ComboObject { Text = "Music", Value = "1" });
combObjList.Add(new ComboObject { Text = "Games", Value = "2" });
combObjList.Add(new ComboObject { Text = "Video", Value = "3" });
cbProduct.DataContext= combObjList;
cbProduct.DisplayMemberPath = "Text";
cbProduct.SelectedValuePath = "Value";
答案 0 :(得分:1)
确保您绑定的属性定义了“get”。
public ObservableCollection<ComboObject> CombObjList
{
get { return combObjList; }
}
private ObservableCollection<ComboObject> combObjList = new ObservableCollection<ComboObject>();
class ComboObject
{
public string Text { get; set; }
public string Value { get; set; }
}
另外,看看你的'输出'窗口,确定你是否有任何绑定错误。
希望这有帮助!
答案 1 :(得分:0)
您是否在组合框上尝试过DisplayMemberPath属性?