ComboBox display not updating when values are changed by code
Posted: 14 Jul 2021, 01:48
Hi, I'm having some difficulty with ComboBoxes. I'm trying to use a command to reset the values of a set of ComboBoxes, but when I use code to change the SelectedValue and SelectedIndex properties of the ComboBoxes, it doesn't seem to change the displayed value at all. I'm not sure if I may be misunderstanding something about how ComboBox works, or if there's some mistake in the code?
Here's the code in question, though I've simplified and stripped it somewhat to focus it on the problem at hand.
Here's the code in question, though I've simplified and stripped it somewhat to focus it on the problem at hand.
Code: Select all
<UserControl
x:Class="DropdownControl"
x:Name="DropdownSelector">
<Grid>
<ComboBox
x:Name="DropdownBox"
Style="{StaticResource DropdownComboBoxStyle}"
SelectedIndex="{Binding SelectedIndex, ElementName=DropdownSelector, Mode=TwoWay}">
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding ElementName=DropdownBox, Path=IsVisible}" Value="False">
<i:InvokeCommandAction Command="{Binding ClearSelectedValues, ElementName=DropdownSelector}" />
</ei:DataTrigger>
</i:Interaction.Triggers>
<ComboBoxItem Content="01" />
<ComboBoxItem Content="02" />
<ComboBoxItem Content="03" />
<ComboBoxItem Content="04" />
<ComboBoxItem Content="05" />
<ComboBoxItem Content="06" />
<ComboBoxItem Content="07" />
<ComboBoxItem Content="08" />
<ComboBoxItem Content="09" />
</ComboBox>
</Grid>
</UserControl>
Code: Select all
class DropdownControl: public Noesis::UserControl
{
public:
DropdownControl();
static const Noesis::DependencyProperty* SelectedValueProperty;
void ExecuteClearSelectedValues(Noesis::BaseComponent* param);
int GetSelectedIndex() const { return mSelectedIndex; }
void SetSelectedIndex(const int value);
private:
Noesis::Ptr<NoesisApp::DelegateCommand> commandClearSelectedValues;
int mSelectedIndex = 0;
int mSelectedValue = 1;
NS_DECLARE_REFLECTION(DropdownControl, Noesis::UserControl)
};
DropdownControl::DropdownControl()
{
commandClearSelectedValues = Noesis::MakePtr<NoesisApp::DelegateCommand>();
commandClearSelectedValues->SetExecuteFunc(Noesis::MakeDelegate(this, &DropdownControl::ExecuteClearSelectedValues));
}
void DropdownControl::ExecuteClearSelectedValues(Noesis::BaseComponent* /* param */)
{
mSelectedIndex = 0;
mSelectedValue = mSelectedIndex + 1;
SetValue<int>(SelectedValueProperty, mSelectedValue);
}
NS_IMPLEMENT_REFLECTION(DropdownControl, "DropdownControl") // NOLINT
{
Noesis::UIElementData* data = NsMeta<Noesis::UIElementData>(Noesis::TypeOf<SelfClass>());
data->RegisterProperty<int>(SelectedValueProperty, "SelectedValue",
Noesis::FrameworkPropertyMetadata::Create(int(0), Noesis::FrameworkPropertyMetadataOptions_None));
NsProp("SelectedIndex", &DropdownControl::GetSelectedIndex, &DropdownControl::SetSelectedIndex);
NsProp("ClearSelectedValues", &DropdownControl::GetClearSelectedValues);
}