2011-01-02 5 views
4

にWPFのリストボックスをバインド。 私はそれが過剰なものだと思うので、私はこれにmvvmを使用しなかった。は私が別のプロジェクトに対して時間エントリを記録するために使用する非常に基本的なWPFアプリケーションを作成したコンボボックス

私は、コンボボックスやリストボックスを含むフォームを持っています。私はプロジェクトにコンボボックスをバインドし、私はコンボボックスから項目を選択するたびに、そのプロジェクトに関連する利用可能なタスクとリストビューを更新している私は何をしようとしています。この alt text

などの基本的なエンティティモデルを作成しました。

これまでの私の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> 

答えて

0

あなたは以下に示すように、ちょうどあなたのItemsSourceを変更するには、グリッドでのDataContextを設定します。

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

Hey Zamboni、 ご返信が遅くなりました。私はあなたの例を次のように試しました プライベートSystem.Data.Objects.ObjectQuery GetTasksQuery(TimeManagementEntities timeManagementEntities) { //選択項目を取得 int id =(int)comProjects.SelectedValue; System.Data.Objects.ObjectQuery tasksQuery = timeManagementEntities.Tasks.Where(タスク=> task.ProjectID == ID)のObjectQueryとして。 return tasksクエリ。 } –

+0

は、しかし、私はが、それはデリゲート型ではないので、ラムダ式は、「文字列」を入力して変換できませんエラーエラーを取得します。何が間違っているかを提案できます –

+0

ファイルの先頭に次の行を追加します。using System.Linq; – Zamboni

関連する問題