dcockerham
Topic Author
Posts: 11
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);
    }
 
User avatar
sfernandez
Site Admin
Posts: 2984
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.
 
[email protected]
Posts: 1
Joined: 08 Sep 2023, 18:55

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

15 Dec 2023, 19:39

Hello! I have a followup question that relates to this.
I currently have a ComboBox where, when I change the language of my game, the current value that is shown does not change language.
My initial fix is to call OnPropertyChanged() twice (the first time is after setting the SelectedItemIndex to -1, the second is after setting the SelectedItemIndex to the original index value).

However, I need to do this for multiple comboboxes in different xaml files. So I think it would be better to have a codebehind for a custom ComboBox that would listen for a signal from a function called OnLanguageChanged(). Upon receiving the signal, it would refresh the SelectedItemIndex.

Could you help me figure out what a codebehind would look like for the custom ComboBox (I'm naming it FilterDropdown)?
Here is what my xaml currently looks like:
                
                <StackPanel>
                  <TextBlock Text="{DynamicResource title}"/>
                  <ComboBox
                    SelectedIndex="{Binding SelectedItemIndex, Mode=TwoWay}">
                    <ComboBoxItem Content="{DynamicResource ex1}"/>
                    <ComboBoxItem Content="{DynamicResource ex2"/>
                    <ComboBoxItem Content="{DynamicResource ex3}"/>
                  </ComboBox>
                </StackPanel>
I also have a viewmodel with this code:
int ExampleViewModel::GetSelectedItemIndex() const {
	return mItemIndex;
}

void ExampleViewModel::SetSelectedItemIndex(int index) {
	mItemIndex = index;
	OnPropertyChanged("SelectedItemIndex");
	UpdateFilteredList();
}
...
	NsProp("SelectedItemIndex", &ExampleViewModel::GetItemIndex,
		&ExampleViewModel::SetItemIndex);
		
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

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

18 Dec 2023, 18:33

I've just tried and this works as expected in WPF: when selected item changes its Content, it automatically updates the parent ComboBox display text. So it is clearly a bug in Noesis, no need to create any custom ComboBox. Could you please report it in our bugtracker?

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 60 guests