我有一个组合框和一个按钮。当我点击按钮时,应该更改选定的值,然后在更改项目时触发代码。不幸的是,在我设置组合框的选定值之前,选择改变了事件触发。使用选定值(C#WPF)时选择更改之前组合框选择更改事件触发
我的组合框的初始化代码:
ListCB.SelectedValuePath = "Key";
ListCB.DisplayMemberPath = "Value";
ListCB.Items.Add(new KeyValuePair<int, string>(1, "One"));
ListCB.SelectedValuePath = "Key";
ListCB.DisplayMemberPath = "Value";
ListCB.Items.Add(new KeyValuePair<int, string>(2, "Two"));
ListCB.SelectedValuePath = "Key";
ListCB.DisplayMemberPath = "Value";
ListCB.Items.Add(new KeyValuePair<int, string>(3, "Three"));
我的按钮的代码:
ListCB.SelectedValue = 3;
我的选择更改事件:
private void ListCB_Change(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("Value is now: " + ListCB.SelectedValue);
}
什么情况是,如果比如我选1从组合框中单击按钮,它会显示现在的值是:一个而不是三个。
我使用了错误的事件吗?还是我用错误的方式来改变选定的值?
ListCB.SelectedIndex按预期工作,但不幸的是,内容是挥发性它不是可行的我的使用。 –