© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
31 replies
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>
<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); 
        });
    }
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;
    }
}
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.
this.
does not have a method called
Find()
Find()
? Thanks
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Avalonia User Controls
C#CC# / help
4y ago
✅ Help with Avalonia Templated Controls
C#CC# / help
3y ago
Conditional User input Form
C#CC# / help
3y ago
❔ ASP.NET Dynamic input controls
C#CC# / help
3y ago