- dcockerham
- Posts: 11
- Joined:
ComboBox display not updating when values are changed by code
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);
}
-
sfernandez
Site Admin
- Posts: 3093
- Joined:
Re: ComboBox display not updating when values are changed by code
Only dependency properties are automatically notifying of changes. You have to expose SelectedIndex as a dependency property or implement INotifyPropertyChanged in your control:
On a side note, if you want to remove any selection you should set the SelectedIndex to -1.
Code: Select all
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>();
...
}
}
- [email protected]
- Posts: 1
- Joined:
Re: ComboBox display not updating when values are changed by code
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:
I also have a viewmodel with this code:
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:
Code: Select all
<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>
Code: Select all
int ExampleViewModel::GetSelectedItemIndex() const {
return mItemIndex;
}
void ExampleViewModel::SetSelectedItemIndex(int index) {
mItemIndex = index;
OnPropertyChanged("SelectedItemIndex");
UpdateFilteredList();
}
...
NsProp("SelectedItemIndex", &ExampleViewModel::GetItemIndex,
&ExampleViewModel::SetItemIndex);
-
sfernandez
Site Admin
- Posts: 3093
- Joined:
Re: ComboBox display not updating when values are changed by code
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: Google [Bot] and 5 guests