2017-11-26 7 views
0

私のViewModelにはPlanTable - List PlanTableのリストがあります。 DayPlanには、DayおよびList ExercisesWithRepsという文字列が含まれています。 ExercisesWithRepsは、文字列の名前とリスト担当者の含まれてい内側のリストボックスアイテムが選択されているときに外側のリストボックスアイテムを選択する方法は?

私は(私はすでにやっている)、この使用して、リストボックスのすべてを表示できるようにしたい:。 screenshot

ときExerciseNameかのいずれかをユーザーがクリックします(数字)、私はこれらの2つのうちの1つだけでなく、1日もキャプチャすることができるようにしたいと思います(ユーザーがtesstの下で12をクリックすると、月曜日も取得します)。火曜日、私は火曜日も選択されたい)。しかし、私はそれを行う方法を理解することはできません...ここで、現時点で

は私のコードです: VIEW:で

 private List<DayPlan> planTable; 
    public List<DayPlan> PlanTable 
    { 
     get { return planTable; } 
     set 
     { 
      planTable = value; 
      RaisePropertyChanged(); 
     } 
    } 
public class DayPlan 
{ 
    public string Day { get; set; } 
    public List<Exercise> ExercisesWithReps { get; set; } 
    private Exercise selectedExerciseName; 
    public Exercise SelectedExerciseName 
    { 
     get { return selectedExerciseName; } 
     set 
     { 
      selectedExerciseName = value; 
      if (value != null) 
      { 
       TempExercise.Name = value.Name; 
      } 
     } 
    } 
} 
    public class Exercise 
{ 
    public string Name { get; set; } 
    public List<int> Reps { get; set; } 
    private int selectedReps; 
    public int SelectedReps 
    { 
     get { return selectedReps; } 
     set 
     { 
      selectedReps = value; 
      TempExercise.Reps = value; 
     } 
    } 
} 

:ここ

<ListBox Name="lbParent" Grid.Row="1" Grid.ColumnSpan="3" ItemsSource="{Binding PlanTable}" SelectedItem="{Binding SelectedListItem, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel HorizontalAlignment="Center"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="*"/> 
         <RowDefinition Height="*"/> 
         <RowDefinition Height="*"/> 
         <RowDefinition Height="*"/> 
        </Grid.RowDefinitions> 
        <StackPanel> 
         <TextBlock Text="{Binding Day}" HorizontalAlignment="Center" Foreground="Black" Grid.Row="0" Margin="10"/> 
         <ListBox ItemsSource="{Binding ExercisesWithReps}" SelectedItem="{Binding SelectedExerciseName, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"> 
           <ListBox.ItemsPanel> 
           <ItemsPanelTemplate> 
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/> 
           </ItemsPanelTemplate> 
          </ListBox.ItemsPanel> 
          <ListBox.ItemTemplate> 
           <DataTemplate> 

            <StackPanel HorizontalAlignment="Center"> 
             <TextBlock Text="{Binding Path=Name}" HorizontalAlignment="Center"/> 
             <ListBox ItemsSource="{Binding Reps}" BorderBrush="Transparent" SelectedItem="{Binding SelectedReps, UpdateSourceTrigger=PropertyChanged}"> 
              <ListBox.ItemsPanel> 
               <ItemsPanelTemplate> 
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/> 
               </ItemsPanelTemplate> 
              </ListBox.ItemsPanel> 
              <ListBox.ItemTemplate> 
               <DataTemplate> 
                <TextBlock Text="{Binding }" HorizontalAlignment="Center"/> 
               </DataTemplate> 
              </ListBox.ItemTemplate> 
             </ListBox> 
            </StackPanel> 
           </DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListBox> 
        </StackPanel> 



       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

のViewModelとDayPlanです私が3つの項目(Day、ExerciseName、およびReps)をすべて選択した場合にのみ、ユーザーの選択を取得できます。また、私は名前をキャプチャし、担当者だ道は非常に不快です...私はそれがViewModelににキャプチャすることができなかった...

+0

TreeViewでもっと簡単になると思います – sTrenat

答えて

0

DayPlanSelectedExerciseNameプロパティが設定されるたびにSelectedListItemプロパティを設定:

planTable = new List<DayPlan>() { ... }; 
foreach(var dayPlan in planTable) 
{ 
    dayPlan.PropertyChanged += (ss, ee) => 
    { 
     if(e.PropertyName == "SelectedExerciseName") 
     { 
      SelectedListItem = dayPlan; 
      RaisePropertyChanged("SelectedListItem"); 
     } 
    }; 
} 

これはINotifyPropertyChangedインタフェースを実装し、SelectedExerciseNameプロパティを新しい値に設定されている場合PropertyChangedイベントを発生させるDayPlanクラスが必要です。また、XAMLのバインディングからMode=OneWayToSourceを削除する必要があります。

+0

Foreachループはどこに行きますか? – Alex

+0

@Alex:ビューモデルのコンストラクタ、またはPlanTableプロパティを初期化する場所。 – mm8

+0

リストにはPropertyChangedの定義が含まれていません...それは私が得たものです – Alex

関連する問題