【问题标题】:Binding a wpf listbox to a combobox将 wpf 列表框绑定到组合框
【发布时间】:2011-01-11 00:24:02
【问题描述】:

我创建了一个非常基本的 wpf 应用程序,我想用它来记录不同项目的时间条目。 我没有为此使用 mvvm,因为我认为它有点矫枉过正。

我有一个包含组合框和列表框的表单。我已经创建了一个像这样的基本实体模型

我要做的是将组合框绑定到项目,每当我从组合框中选择一个项目时,它都会使用与该项目关联的可用任务更新列表视图。

到目前为止,这是我的 xaml。我没有任何代码,因为我只是单击了该数据菜单,然后单击数据源并将项目拖放到上面。应用程序加载正常,组合框已填充,但列表框中没有显示任何内容。

谁能告诉我我错过了什么?

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

【问题讨论】:

    标签: wpf entity-framework-4


    【解决方案1】:

    您在 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 GetTasksQuery(TimeManagementEntities timeManagementEntities) { // 获取所选项目 int id = (int)comProjects.SelectedValue; System.Data.Objects.ObjectQuery tasksQuery = timeManagementEntities.Tasks.Where(task => task.ProjectID == id) as ObjectQuery;返回任务查询; }
    • 但是我收到一个错误错误 1 ​​无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型。你能提出什么问题吗
    • 在文件顶部添加以下行:using System.Linq;
    猜你喜欢
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多