我有一个包含一定年限的ComboBox
。但是,我的项目有问题,应该选择。
XAML
<ComboBox MaxDropDownHeight="90" Height="10" Width="80" ItemsSource="{Binding accy}" DisplayMemberPath="DisplayMember" SelectedValuePath="ValuePath" SelectedValue="{Binding Accy_selected}"/>
MainWindow.cs
public MainWindow()
{
InitializeComponent();
this.DataContext = new Test();}
test.cs中
class Test
{
public ObservableCollection<ComboYear> accy { get; set; }
private string _accy_selected = "";
public Test()
{
int min = 1940;
int max = 2040;
accy = new ObservableCollection<ComboYear>();
for (int i = min; i < max; i++)
{
accy.Add(new ComboYear { DisplayMember = Convert.ToString(i), ValuePath = Convert.ToString(i) });
}
Accy_selected = Convert.ToString(max);
}
public class ComboYear
{
public string DisplayMember { get; set; }
public string ValuePath { get; set; }
}
public string Accy_selected
{
get { return _accy_selected; }
set
{
if (value != _accy_selected)
{
_accy_selected = value;
NotifyPropertyChanged("Accy_selected");
}
}
}
ComboBox
被填满,但没有选择任何项目。关于这一点的奇怪之处在于我是否改变了这一行
Accy_selected = Convert.ToString(max);
它正在发挥作用。它每年都在ComboBox
工作。将int
转换成string
我真傻吗?
Accy_selected = "2039";
我不知道如何解决它
qazxsw poi(qazxsw poi)不包括在qazxsw poi中,是吗?只要计数器变量max
小于2040
,你就在迭代:
ComboBox
如果将条件更改为i
,则在设置max
属性时应包含并选择值“2040”:
for (int i = min; i < max; i++)