将WPF ListBox绑定到ComboBox

4
我创建了一个非常基本的WPF应用程序,用于记录不同项目的时间条目。我没有使用MVVM,因为我认为这是一种过度设计。
我有一个包含ComboBox和ListBox的表单。我创建了一个基本的实体模型,如下所示: alt text 我想绑定ComboBox到Project,并且每当我从ComboBox中选择一个项目时,它会更新ListView,显示与该项目相关联的可用任务。
这是我的XAML代码。我没有任何代码后台,因为我只是点击了Data菜单,然后数据源并将项目拖放到了应用程序中。应用程序可以正常加载,ComboBox已经填充,但是ListBox中没有显示任何内容。
请问我错过了什么?
    <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>

1个回答

0

您在Grid中设置了DataContext,因此只需按下面所示更改您的ItemsSource。

<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>

您还需要过滤任务列表,只需更改生成的代码即可。这是我拼凑出来的一个示例。您可以使用 ComboBox 的 SelectionChanged 事件切换值。

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;
}

嘿,Zamboni, 抱歉回复晚了。我尝试了你的示例代码: private System.Data.Objects.ObjectQuery<Task> GetTasksQuery(TimeManagementEntities timeManagementEntities) { // 获取所选项 int id = (int)comProjects.SelectedValue; System.Data.Objects.ObjectQuery<NotificationAreaApplication1.Task> tasksQuery = timeManagementEntities.Tasks.Where(task => task.ProjectID == id) as ObjectQuery<NotificationAreaApplication1.Task>; return tasksQuery; } - Diver Dan
然而我遇到了一个错误:错误1 无法将lambda表达式转换为类型“string”,因为它不是委托类型。你能建议一下出了什么问题吗? - Diver Dan
在你的文件顶部添加以下一行代码:using System.Linq; - Zamboni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接