dcockerham
Topic Author
Posts: 3
Joined: 10 Apr 2021, 00:44

ComboBox display not updating when values are changed by code

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.
<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>
    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);
    }

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2255
Joined: 22 Dec 2011, 19:20

Re: ComboBox display not updating when values are changed by code

15 Jul 2021, 19:08

Only dependency properties are automatically notifying of changes. You have to expose SelectedIndex as a dependency property or implement INotifyPropertyChanged in your control:
class DropdownControl: public Noesis::UserControl, public Noesis::INotifyPropertyChanged
{
  ...
  PropertyChangedEventHandler& PropertyChanged() { return mChanged; }
  Noesis::PropertyChangedEventHandler mChanged;
  ...
  void ExecuteClearSelectedValues(Noesis::BaseComponent* /* param */)
  {
    mSelectedIndex = 0;
    mChanged(this, NsSymbol("SelectedIndex"));
    
    mSelectedValue = mSelectedIndex + 1;
    SetValue<int>(SelectedValueProperty, mSelectedValue);
  }
  ...
  NS_IMPLEMENT_INLINE_REFLECTION(DropdownControl, "DropdownControl") // NOLINT
  {
    NsImpl<INotifyPropertyChanged>();
    ...
  }
}
On a side note, if you want to remove any selection you should set the SelectedIndex to -1.

Who is online

Users browsing this forum: No registered users and 1 guest