gpt4 book ai didi

WPF:如何在代码中绑定(bind) ComboBox ItemsSource?

转载 作者:行者123 更新时间:2023-12-04 06:23:47 30 4
gpt4 key购买 nike

我需要将以下 XAML 转换为代码隐藏:

<ComboBox SelectedItem="{Binding Level}" ItemsSource="{Binding Levels}" />

但是,此代码无法编译:
new ComboBox() { SelectedItem = new Binding("Level"), ItemsSource = new Binding("Levels") }

错误:“无法将类型'System.Windows.Data.Binding'隐式转换为'System.Collections.IEnumerable'。存在显式转换(您是否缺少强制转换?)”。我该如何投?

最佳答案

ComboBox cbo=new ComboBox();
cbo.SetBinding(ComboBox.SelectedItemProperty,new Binding("Level"){ /* set properties here*/});
cbo.SetBinding(ComboBox.ItemsSourceProperty,new Binding("Levels"));
....

关于WPF:如何在代码中绑定(bind) ComboBox ItemsSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6252898/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com