我在c ++ windows通用平台上工作。我需要访问组合框项目并更改其属性。所以在OnDropDownOpened方法中,我试图访问组合框项目并设置属性。但是我在第一次打开的时候得到的是comboboxitem null。我可以看到combox中有项目。这是我的代码:
void MainPage::OnDropDownOpened(Platform::Object^ sender, Platform::Object^ e)
{
ComboBox^ combobox = dynamic_cast<ComboBox^>(sender);
int i = 0;
for (auto item : combobox->Items)
{
ComboBoxItem^ comboboxItem;
if (i >= 0)
{
comboboxItem = dynamic_cast<ComboBoxItem^>(combobox->ContainerFromIndex(i));
if (comboboxItem !=nullptr)
{
comboboxItem->IsEnabled = true;
comboboxItem->FontWeight = Windows::UI::Text::FontWeights::Bold;
}
}
++i;
}
}
当我点击下拉列表时,我可以看到comboboxItem为NULL,但我在组合框中有项目。从第二次点击开始,它工作正常。这里的 ContainerFromIndex 有什么问题?
答案 0 :(得分:1)
对发件人对象的UpdateLayout调用对我有用:
void MainPage::OnDropDownOpened(Platform::Object^ sender, Platform::Object^ e)
{
ComboBox^ combobox = dynamic_cast<ComboBox^>(sender);
combobox->UpdateLayout();
int i = 0;
for (auto item : combobox->Items)
{
ComboBoxItem^ comboboxItem;
if (i >= 0)
{
comboboxItem = dynamic_cast<ComboBoxItem^>(combobox->ContainerFromIndex(i));
if (comboboxItem !=nullptr)
{
comboboxItem->IsEnabled = true;
comboboxItem->FontWeight = Windows::UI::Text::FontWeights::Bold;
}
}
++i;
}
}