Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Winui 3 Combobox in datagrid not displaying data

Post

Winui 3 Combobox in datagrid not displaying data

+2
−0

I am migrating a WPF app to WinUI3. Things went well until I needed to put a combo box in the datagrid. That MS had removed the datagrid from the standard tools was overcome with Community Toolkit Controls.

I can make a column with a combobox, but I have not managed to link it correctly to the data I need to select from. Let's say that it is a page to display books with their primary genre in the combobox.

I use MVVM for connecting the view and data. Showing the main part of the data for the page works. However, the combobox for genres is empty.

The window:

<Grid x:Name="RootGrid">
    <ctWUI3:DataGrid x:Name="tasksDataGrid"
                     AutoGenerateColumns="False" 
                     GridLinesVisibility="All"
                     CanUserSortColumns="True"
                     ScrollViewer.VerticalScrollBarVisibility="Auto"
                     AlternatingRowBackground="LightGray"
                     ItemsSource="{Binding Books}" 
                     Margin="10,0,5,0" 
                     RowDetailsVisibilityMode="VisibleWhenSelected">
        <ctWUI3:DataGrid.Columns>
            <ctWUI3:DataGridTextColumn Binding="{Binding Id}"
                        		Header="Name" 
								Width="150"
                        		IsReadOnly="False" />
            <ctWUI3:DataGridTextColumn Binding="{Binding Title}"
                        		Header="Name" 
								Width="150"
                        		IsReadOnly="False" />
            <ctWUI3:DataGridTextColumn Binding="{Binding Description}"
                        		Header="Description" 
								Width="150"
                        		IsReadOnly="False" />
            <ctWUI3:DataGridTextColumn Binding="{Binding PrimaryGenreId}"
                        		Header="Genre" 
								Width="150"
                        		IsReadOnly="False" />
            <ctWUI3:DataGridComboBoxColumn Binding="{Binding PrimaryGenreId}"
                                           Header="Genre" 
                                           ItemsSource="{Binding Genres, Mode=OneWay}"
                                           DisplayMemberPath="Name"
                                           Width="SizeToHeader" />
        </ctWUI3:DataGrid.Columns>
    </ctWUI3:DataGrid>
</Grid>

The xaml.cs

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
        RootGrid.DataContext = new MainViewModel();
    }
}

Then the MainViewModel.cs contains two private fields, a constructor of test data, and the public getter/setters for the fields.

public ObservableCollection<Book> Books
{
    get => _books;
    set { SetProperty(ref _books, value); }
}

public ObservableCollection<Genre> Genres
{
    get => _genres;
    set { SetProperty(ref _genres, value); }
}

How do I get the combobox to display the genre of the book for that row (using a GenreId field as the foreign key)?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

What actually does happen in your combo when it runs? Is it just empty? (1 comment)
Avalonia (1 comment)
Avalonia
FoggyFinder‭ wrote 6 months ago

Not related to the Q but have you considered using Avalonia instead of WinUi?