2009-03-19 9 views
0

(4番目の)ソースリストボックスから3つのリストボックスのいずれかを繁殖しようとしています。ソースには、初級、中級、または高校の科目に分類される科目のリストがあります。ソースリストボックスは、チェックボックスのリストです。ユーザーがチェックボックスをクリックすると、他の3つのうちの1つは、ソースリストからSubjectオブジェクトのコピーを取得することを意図しています。私はその事を結んで、CheckBox_Changedメソッドに成功しました。私は正常にソースリストからSubjectインスタンスを探し、それをターゲットリストのSource配列に追加できます。バインドされたメモリ内コレクションが新しいメンバーを取得したときにSilverLightリストを更新する

対象の配列がバインドされているSilverlightコントロールに更新プログラムを表示することはできません。

アイデア?

おかげでここ

private void CheckBox_Checked(object sender, RoutedEventArgs e) 
{ 
    var cb = (CheckBox)sender; 
    var children = ((Grid)cb.Parent).Children; 
    // cb has a sibling TextBlock item that has the index of the item in 
    // the list of subjects 
    var ch2 = children[1] as TextBlock; 
    var subjectIndexStr = ch2.Text; 
    var myWorkingSubject = workingSubjectList[int.Parse(subjectIndexStr)]; 

    switch (myWorkingSubject.SubjectLevelId) 
    { 
// updates to the elementarySubjects, middleSubjects and highSubjects 
// don't get reflected in the lists that use them as a resource. 
    case (int)SubjectLevels.Elementary: 
     elementarySubjects.Add(myWorkingSubject); 
     break; 
    case (int)SubjectLevels.Middle: 
     middleSubjects.Add(myWorkingSubject); 
     break; 
    case (int)SubjectLevels.High: 
     highSubjects.Add(myWorkingSubject); 
     break; 
    default: break; 

    } 
} 

// this is how the target classes are declared. 
public class SubjectsElementary : ObservableCollection<WorkingSubject> 
{ 
} 
public class SubjectsMiddle : ObservableCollection<WorkingSubject> 
{ 
} 
public class SubjectsHigh : ObservableCollection<WorkingSubject> 
{ 
} 

私はかなりわからないんだけど、これはDispatch.BeginInvoke内部の変更を行うことによって修正可能かもしれの.xamlファイルからの抜粋は

<TutorRouterSvc:WorkingSubjectList x:Key="subjects" /> 
<TutorRouterSvc:SubjectsElementary x:Key="elementarySubjects" /> 
<TutorRouterSvc:SubjectsMiddle x:Key="middleSubjects" /> 
<TutorRouterSvc:SubjectsHigh x:Key="highSubjects" /> 


    <ListBox x:Name="subjectList" ItemsSource="{Binding Mode=OneWay, Source={StaticResource subjects}}"> 

    <ListBox.Resources> 

    </ListBox.Resources> 
     <ListBox.ItemTemplate> 
      <StaticResource ResourceKey="DataSubjectsTemplate1"/> 
     </ListBox.ItemTemplate> 
    </ListBox> 

<Grid Grid.Column="1"> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="*"/> 
    <RowDefinition Height="*"/> 
    <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <ListBox Margin="0,0,8,0" x:Name="elementarySubjectList" 
      ItemsSource="{Binding Mode=OneWay, Source={StaticResource elementarySubjects}}" 
      Background="#FFE75151" Grid.Row="0"> 
    <ListBox.ItemTemplate> 
     <StaticResource ResourceKey="DataSubjectsTemplate1"/> 
    </ListBox.ItemTemplate> 
    </ListBox> 
    <ListBox Margin="0,0,8,0" x:Name="middleSubjectList" 
      ItemsSource="{Binding Mode=OneWay, Source={StaticResource middleSubjects}}" 
      Background="#FFE75151" Grid.Row="1"> 
    <ListBox.ItemTemplate> 
     <StaticResource ResourceKey="DataSubjectsTemplate1"/> 
    </ListBox.ItemTemplate> 
    </ListBox> 
    <ListBox Margin="0,0,8,0" x:Name="highSubjectList" 
      ItemsSource="{Binding Mode=OneWay, Source={StaticResource highSubjects}}" 
      Background="#FFE75151" Grid.Row="1"> 
    <ListBox.ItemTemplate> 
     <StaticResource ResourceKey="DataSubjectsTemplate1"/> 
    </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

答えて

0

です()。

あなたは新しいメソッドにswitch文をリファクタリング可能性は、それを呼び出し、UpdateListBoxと呼ばれる:

Dispatcher.BeginInvoke(() => UpdateListBox(myWorkingSubject.SubjectLevelId)) 
+0

お返事ありがとうございます。 Dispatcher.BeginInvoke()がキュー上でデリゲートを開始する以外は何をするのか分かりません。バインドされたデータコレクションに新しい要素を追加するだけで十分だろうと私は考えていました。 UpdateListBox()はListBoxを更新するために何をしますか? – Weej

0

XAMLはそれがデータバインディングだあなたのオブジェクトの新しいインスタンスNEWINGアップしているので、多分これが起こっています。

Page.xaml.csのcosntructorにこれを追加してみてください。

_subjects = Resources["subjects"] as WorkingSubjectsList; 
_elementarySubjects = Resources["elementarySubjects"] as SubjectsElementary; 

等...を助けるかもしれない

。私はいくつかの機会にリストボックスをObservableコレクションにバインドして同じコンセプトを実装しましたが、あなたが遭遇していることを経験していません。

私はいくつかの提案があります: あなたはチェックチェンジしたイベントでこれを試しましたか?

workingsubject _item = workingSubjectList[subjectsList.selectedindex]; 
switch (_item.SubjectLevel) //I'm assuming this property as you have the ID and it looks to be an enumeration 
     { 
      case Elementary: 
       elementarySubjects.Add(_item): 
       break; 
      case Middle: 
       middleSubjects.Add(_item): 
       break; 
      case High: 
       highSubjects.Add(_item): 
       break; 
       case default: 
         throw new Exception("Unrecognized Subject Level"); 
     } 

hth。

関連する問題