Associate a wpf list with a list

I created a very basic wpf application that I want to use to record time records for different projects. I did not use mvvm for this, since I think it is overkill.

I have a form containing combobox and listbox. I created a basic entity model similar to this alt text

What I'm trying to do is associate a combobox with a Project and whenever I select an item from combobox, it updates the list with available tasks related to this project.

This is my xaml so far. I have no code, because I just clicked on this data menu, and then on the data sources and dragged items. The application loads normally, and the combo box is full, but nothing appears in the list.

Can someone tell me what I missed?

<Window.Resources> <CollectionViewSource x:Key="tasksViewSource" d:DesignSource="{d:DesignInstance l:Task, CreateList=True}" /> <CollectionViewSource x:Key="projectsViewSource" d:DesignSource="{d:DesignInstance l:Project, CreateList=True}" /> </Window.Resources> <Grid DataContext="{StaticResource tasksViewSource}"> <l:NotificationAreaIcon Text="Time Management" Icon="Resources\NotificationAreaIcon.ico" MouseDoubleClick="OnNotificationAreaIconDoubleClick"> <l:NotificationAreaIcon.MenuItems> <forms:MenuItem Text="Open" Click="OnMenuItemOpenClick" DefaultItem="True" /> <forms:MenuItem Text="-" /> <forms:MenuItem Text="Exit" Click="OnMenuItemExitClick" /> </l:NotificationAreaIcon.MenuItems> </l:NotificationAreaIcon> <Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="150,223,0,0" Name="btnInsert" VerticalAlignment="Top" Width="46" Click="btnInsert_Click" /> <ComboBox Height="23" HorizontalAlignment="Left" Margin="70,16,0,0" Name="comProjects" VerticalAlignment="Top" Width="177" DisplayMemberPath="Project1" ItemsSource="{Binding Source={StaticResource projectsViewSource}}" SelectedValuePath="ProjectID" /> <Label Content="Projects" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" IsEnabled="False" /> <Label Content="Tasks" Height="28" HorizontalAlignment="Left" Margin="16,61,0,0" Name="label2" VerticalAlignment="Top" /> <ListBox Height="112" HorizontalAlignment="Left" Margin="16,87,0,0" Name="lstTasks" VerticalAlignment="Top" Width="231" DisplayMemberPath="Task1" ItemsSource="{Binding Path=ProjectID, Source=comProjects}" SelectedValuePath="TaskID" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="101,224,0,0" Name="txtMinutes" VerticalAlignment="Top" Width="42" /> <Label Content="Mins to Insert" Height="28" HorizontalAlignment="Left" Margin="12,224,0,0" Name="label3" VerticalAlignment="Top" /> <Button Content="None" Height="23" HorizontalAlignment="Left" Margin="203,223,0,0" Name="btnNone" VerticalAlignment="Top" Width="44" /> </Grid> 

+4
source share
1 answer

You set the DataContext in the Grid, so just change your ItemsSource as follows.

 <Grid DataContext="{StaticResource tasksViewSource}"> <ListBox Height="112" HorizontalAlignment="Left" Margin="16,87,0,0" Name="lstTasks" VerticalAlignment="Top" Width="231" DisplayMemberPath="Task1" ItemsSource="{Binding}" SelectedValuePath="TaskID" /> </Grid> 

You will also want to filter out the task list to simply modify the generated code. Here is an example that I hacked. You can switch the value using the SelectionChanged event from your ComboBox.

 private System.Data.Objects.ObjectQuery<Models.Task> GetTasksQuery(Models.StackoverflowEntities stackoverflowEntities) { // get the selected item int id = (int)cboProjects.SelectedValue; System.Data.Objects.ObjectQuery<CollectionViewSourceEF.Models.Task> tasksQuery = stackoverflowEntities.Tasks.Where(task => task.ProjectID == id) as ObjectQuery<Task>; return tasksQuery; } 
0
source

Source: https://habr.com/ru/post/1333966/


All Articles