Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoSuggestBox text property twoway binding error #731

Closed
agonzalezrubio opened this issue Aug 15, 2023 · 3 comments
Closed

AutoSuggestBox text property twoway binding error #731

agonzalezrubio opened this issue Aug 15, 2023 · 3 comments
Labels
bug Something isn't working controls Changes to the appearance or logic of custom controls. locked-due-to-inactivity

Comments

@agonzalezrubio
Copy link

Describe the bug

Typing into the AutoSuggestBox text with a binding gives me this error

  <ui:AutoSuggestBox 
      x:Name="AdvancedTxtServer"
      Text="{Binding ServerSetup.ServerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
      OriginalItemsSource="{Binding ServerList}" Icon="{ui:SymbolIcon Empty}"/>
This exception was originally thrown at this call stack:
    Wpf.Ui.Controls.AutoSuggestBox.TextPropertyChangedCallback(System.Windows.DependencyObject, System.Windows.DependencyPropertyChangedEventArgs) in AutoSuggestBox.cs
    System.Windows.DependencyObject.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs)
    System.Windows.FrameworkElement.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs)
    System.Windows.DependencyObject.NotifyPropertyChange(System.Windows.DependencyPropertyChangedEventArgs)
    System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex, System.Windows.DependencyProperty, System.Windows.PropertyMetadata, System.Windows.EffectiveValueEntry, ref System.Windows.EffectiveValueEntry, bool, bool, System.Windows.OperationType)
    System.Windows.DependencyObject.SetValueCommon(System.Windows.DependencyProperty, object, System.Windows.PropertyMetadata, bool, bool, System.Windows.OperationType, bool)
    System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty, object)
    MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(object, System.Xaml.XamlMember, object)

image

To Reproduce

Typing into the AutoSuggestBox text with a twoway binding

Expected behavior

No error

Screenshots

image

OS version

Windows 11

.NET version

.NET 7.0

WPF-UI NuGet version

3.0.0-preview.5

Additional context

No response

@agonzalezrubio agonzalezrubio added the bug Something isn't working label Aug 15, 2023
@agonzalezrubio
Copy link
Author

image

@Redvo
Copy link
Contributor

Redvo commented Aug 16, 2023

This happens because TextPropertyChangedCallback is static and called before TextBox is initialized in OnApplyTemplate.

From documentation on Text property:

/// <remarks>
/// This property is not typically set in XAML.
/// </remarks>

So probably a NULL check would be sufficient to not get exception there.

Now for your problem, what are you trying to achieve?

If you want to give AutoSuggestBox a default value, that is probably not a good idea and won't work. Changes in AutoSuggestBox source code are required for that.

If you want to update your ServerSetup.ServerName property when some text is typed into AutoSuggesBox, use OneWayToSource binding. This ensures the ServerName property is always updated when AutoSuggestBox text changes, but not other way around and it won't raise NullReferenceException at the begining.

If you want to set Property in your ViewModel after some option is chosen, that is only possible in code-behind in SuggestionChosen event for now, as far as I discovered:

<ui:AutoSuggestBox
  x:Name="AutoSuggestBox"
  SuggestionChosen="AutoSuggestBox_OnSuggestionChosen" ... />
private void AutoSuggestBox_OnSuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
  ViewModel.ChosenOptionProperty = args.SelectedItem as MyOptionsItemType;
}

@pomianowski pomianowski added the controls Changes to the appearance or logic of custom controls. label Aug 16, 2023
@pomianowski pomianowski pinned this issue Aug 16, 2023
@agonzalezrubio
Copy link
Author

Ohhhh, ok, thank you for your very useful response friend. Sorry I missed that! Lol.

@pomianowski pomianowski unpinned this issue Aug 19, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working controls Changes to the appearance or logic of custom controls. locked-due-to-inactivity
Projects
None yet
Development

No branches or pull requests

3 participants