You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ComboxBox SelectedItem Instantly ConvertBack to null after passing a seemingly valid value.
#18834
Currently encountering an issue with the ComboBox Control.
Currently, i'm passing a JToken from newtonsoft to a Converter, which is then converter to the appropriate object depending on its Type Property.
usingSystem;usingSystem.Globalization;usingAvalonia.Data.Converters;usingNewtonsoft.Json.Linq;namespaceOpenTabletDriver.External.Avalonia.Converters;publicclassJTokenConverter:IValueConverter{publicobject?Convert(object?value,TypetargetType,object?parameter,CultureInfoculture){if(valueis not JTokentoken)returnnull;returntoken.Typeswitch{JTokenType.Boolean=>token.Type!=JTokenType.Null&&token.ToObject<bool>(),JTokenType.Float=>token.Type==JTokenType.Null?0:token.ToObject<double>(),JTokenType.Integer=>token.Type==JTokenType.Null?0:token.ToObject<double>(),JTokenType.String=>token.Type==JTokenType.Null?string.Empty:token.ToObject<string>(),
_ =>null};}publicobject?ConvertBack(object?value,TypetargetType,object?parameter,CultureInfoculture){returnvalue==null?JValue.CreateNull():JToken.FromObject(value);}}
For example, let's say i pass "Choice 1" as a JToken, Convert() will return a string.
Now, The ComboBox itself is contained within a TemplatedControl, and appear as such :
ValidatedPropertyConverter just returns the Values Property (of type IEnumerable) for the passed Child Type SerializableValidatedProperty.
I do see the following error being thrown in the Debug Console :
[Binding]Error in binding to 'OpenTabletDriver.External.Avalonia.TemplatedControls.EnumInput'.'SelectedItem': 'Unable to convert object 'Choice 1' of type 'System.String' to type 'Newtonsoft.Json.Linq.JToken'.'(EnumInput #7469042)
Whic hdoesn't make any sense as ItemSource is indeed a string[], declared as follow :
AFAIK, the SelectedItem must be within the ItemsSource or bad things happen (resets). See #18635. When you write a converter, you're creating different object instances than what the collection contains. I think this scenario may be solved by DisplayMemberBinding is for, but I haven't really used it.
In this case, you'd probably use DisplayMemberBinding="{Binding ., Converter={...}}" instead of actually binding to a member of the item being displayed. I assume you only want this for additional display. You should also be able to give your JTokenType a DisplayName property and do DisplayMemberBinding="{Binding DisplayName}", if you want to go that route.
There's fair number of pitfalls with making dynamic type wrappers. Sometimes it's a requirement, sometimes it's devs being lazy to avoid mapping to types friendly for the Presentation layer. You're going to have issues if this type has mutability and no INotifyPropertyChanged.
That wouldn't work without me putting all the stuff in a separate viewmodel, and even then, JToken is a newtonsoft type, i can't modify it.
Another Solution which would work for my use case is to just change the Binding's value manually when the ComboBox value has changed :
protectedoverridevoidOnApplyTemplate(TemplateAppliedEventArgse){// Workaround for Binding issuevarcomboBox=e.NameScope.Get<ComboBox>("PART_Input");comboBox.SelectionChanged+=(sender,args)=>{if(ItemsSource==null)return;SelectedItem=comboBox.SelectedItem;SelectedIndex=comboBox.SelectedIndex;};base.OnApplyTemplate(e);}
Which, along with disposing & Forced GC of viewmodels to prevent a rogue conversion back to null while doing nothing, does make it work as intended (Setting.Value is synchronized, even when i replace the JToken).
Only thing is, this isn't proper, but it's the only thing that would really work without having to decompose the array & selected value into their own ViewModel & back when needed, which would end up making this project creep into serialization / deserialization hell.
After some research, it turns out ComboBox have issues with TemplatedControl's TemplateBinding.
Switching to a UserControl makes it work without any issues.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Currently encountering an issue with the ComboBox Control.
Currently, i'm passing a
JTokenfrom newtonsoft to a Converter, which is then converter to the appropriate object depending on its Type Property.For example, let's say i pass
"Choice 1"as a JToken,Convert()will return a string.Now, The ComboBox itself is contained within a TemplatedControl, and appear as such :
This TemplatedControl is styled in the following manners in the Generating Control :
ValidatedPropertyConverterjust returns the Values Property (of type IEnumerable) for the passed Child TypeSerializableValidatedProperty.I do see the following error being thrown in the Debug Console :
Whic hdoesn't make any sense as ItemSource is indeed a string[], declared as follow :
So i'm left wondering why this isn't working as i expect it to.
All reactions