【发布时间】:2019-11-18 18:01:15
【问题描述】:
我在调度模块中有几个组合框,它们都有基于“活动”字段的下拉列表。
public class Project
{
public int ProjectID { get; set; }
public int ProjectTitle { get; set; }
public bool Active { get; set; }
}
<ComboBox
Name="ProjectComboBox"
ItemsSource="{Binding AllProjects}"
SelectedItem="{Binding Project, Mode=TwoWay}">
</ComboBox>
日历的编辑表单必须始终在其组合框中显示旧信息,即使组合列表中的特定项目已被停用。但如果打开下拉菜单,它必须只显示列表中仍然处于活动状态的项目。
我将如何做到这一点?
我已经在代码隐藏中尝试过:
private void ProjectComboBox_DropDownOpened(object sender, EventArgs e)
{
ProjectComboBox.SetBinding(ItemsControl.ItemsSourceProperty, "ActiveProjects");
}
private void ProjectComboBox_DropDownClosed(object sender, EventArgs e)
{
ProjectComboBox.SetBinding(ItemsControl.ItemsSourceProperty, "AllProjects");
}
在下拉列表中显示正确的列表,但取消选择最初选择的项目。如果用户没有选择新项目,当下拉菜单关闭时,组合框需要保留原来的选择。
【问题讨论】:
-
所以你失去了选择。嗯..让我想想.. ;) ... 如果您在重置
DropDownOpend事件处理程序中的ComboBox.ItemsSource之前注意到选择ComboBox.SelectedItem并在重置ComboBox.ItemsSource中的ComboBox.ItemsSource事件处理程序之后设置它,然后你也可以达到目标。但 ASh 的解决方案更可取,因为没有代码。