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

ComboBox in collection control doesn't display initial value #17230

Open
Earlh21 opened this issue Oct 9, 2024 · 4 comments
Open

ComboBox in collection control doesn't display initial value #17230

Earlh21 opened this issue Oct 9, 2024 · 4 comments
Labels
by-design The behavior reported in the issue is actually correct.

Comments

@Earlh21
Copy link

Earlh21 commented Oct 9, 2024

Describe the bug

When you have a collection control bound to an ObservableCollection of models, ComboBoxes inside the control don't display the initial value of the model properties they're bound to.

This occurs regardless of when or how items are added to the collection or what the type of the ComboBox values are.

To Reproduce

  1. Create a observable model class TestModel with a string property.
  2. Create a view model with an ObservableCollection[string] property of sample values, and an ObservableCollection[TestModel] property.
  3. In the view model's constructor, add a TestModel to the collection.
  4. In the view's XAML, create an ItemsControl bound to the ObservableCollection[TestModel].
  5. In the ItemsControl's data template, add a ComboBox with ItemsSource bound to the sample values, and SelectedItem bound to TestModel's string property.
  6. Run the app and observe the lack of initial value in the ComboBox.

Example repo showing the issue for two data types (string and enum), and a working ComboBox that isn't based on a collection: https://github.com/Earlh21/ComboBoxTest. There's also a button to add more TestModels dynamically - their ComboBoxes also lack initial values.

Expected behavior

I expected to see the TestModel's initial string property value in the ComboBox.

Avalonia version

11.1.0

OS

Windows

Additional context

No response

@Earlh21 Earlh21 added the bug label Oct 9, 2024
@rabbitism
Copy link
Contributor

Your repo is private

@Earlh21
Copy link
Author

Earlh21 commented Oct 10, 2024

Your repo is private

It's public now.

@timunie timunie added by-design The behavior reported in the issue is actually correct. and removed bug needs-repro needs-author-action labels Oct 15, 2024
@timunie
Copy link
Contributor

timunie commented Oct 15, 2024

@Earlh21 now I see. What happens here is, that the DataTemplate is not a child of MainWindow when it is build and thus it has no Items. No Items --> Selected item is being reset. To overcome this, I suggest to give your Window a name (Root for example) and bind to it via named element syntax:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:ComboBoxTest.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="ComboBoxTest.Views.MainWindow"
        x:DataType="vm:MainWindowViewModel"
        Icon="/Assets/avalonia-logo.ico"
+       x:Name="Root"
        Title="ComboBoxTest">

    <Design.DataContext>
        <!-- This only sets the DataContext for the previewer in an IDE,
             to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
        <vm:MainWindowViewModel/>
    </Design.DataContext>
    
    
    <StackPanel Orientation="Vertical">
        <ComboBox ItemsSource="{Binding EnumValues}" SelectedItem="{Binding ModelEnumProperty.EnumProperty}"/>
        
        <ItemsControl ItemsSource="{Binding EnumModels}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
!                   <ComboBox ItemsSource="{Binding #Root.((vm:MainWindowViewModel)DataContext).EnumValues}"
                              SelectedItem="{Binding EnumProperty}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

+[...]

From what I can tell, this is by design. However will ask the team before closing this issue.

@Earlh21
Copy link
Author

Earlh21 commented Oct 15, 2024

Right, I didn't even think about the relative reference being the issue. Yes that works, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
by-design The behavior reported in the issue is actually correct.
Projects
None yet
Development

No branches or pull requests

3 participants