C#C
C#3y ago
Mek

✅ conditional input between two controls avalonia

<ComboBox
    Name="SelectUser"
    Classes="SelectUserCombo"
    Grid.Row="1"
    Grid.Column="0"
    SelectedItem="{Binding SelectedUsername}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
public partial class SelectUserViewModel : ViewModelBase
{
    public List<string> _usernames;
    public string? _selectedUsername;
    public string? _newUser;
    public IObservableList<User?>? _currentUsers;

    public SelectUserViewModel()
    {
        AvaloniaXamlLoader.Load(this);

        CurrentUsers = UserQuery.GetUsernames();
        ComboBox comboBox = this.
        comboBox.ItemsSource = CurrentUsers;

        IObservable<bool> selectedUsernameOk = this.WhenAnyValue(
            x => x.SelectedUsername,
            x => !string.IsNullOrEmpty(x));

        IObservable<bool> okEnabled = selectedUsernameOk;

        Ok = ReactiveCommand.Create(
            ReturnSelectedUser,
            okEnabled);
        Cancel = ReactiveCommand.Create(() => { 
            Environment.Exit(0); 
        });
    }
I'm trying to load a list of usernames into a combo box (if any exists) when the page loads in the pages ViewModel. I know I can do it in the code behind like this
public partial class SelectUserView : UserControl
{
    private List<User?>? Usernames;

    public SelectUserView()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
        Usernames = UserQuery.GetUsernames();
        ComboBox comboBox = this.Find<ComboBox>("SelectUser");
        comboBox.ItemsSource = Usernames;
    }
}
however, that's not best practice. The username that the user selects from the combo box is bound to SelectedUsername. The usernames shown within the combobox are loaded into an ObservableList CurrentUsers. How would I load that list into the combobox since the ViewModel says that
this.
does not have a method called
Find()
? Thanks
Was this page helpful?