Data binding 当绑定内存集合获得新成员时更新SilverLight列表
我试图从(第四个)源列表框中提取三个列表框中的一个。资料来源有一份学校科目清单,分为小学、中学或高中科目。源列表框是复选框列表。用户单击复选框,其他三个复选框中的一个用于从源列表中获取主题对象的副本。我已经把它连接好了,并且成功地点击了一个复选框。我可以成功地从源列表中找到主题实例,并将其添加到目标列表的源数组中 我不能做的是显示目标数组绑定到的Silverlight控件的更新 有什么想法吗 谢谢Data binding 当绑定内存集合获得新成员时更新SilverLight列表,data-binding,listbox,silverlight-2.0,Data Binding,Listbox,Silverlight 2.0,我试图从(第四个)源列表框中提取三个列表框中的一个。资料来源有一份学校科目清单,分为小学、中学或高中科目。源列表框是复选框列表。用户单击复选框,其他三个复选框中的一个用于从源列表中获取主题对象的副本。我已经把它连接好了,并且成功地点击了一个复选框。我可以成功地从源列表中找到主题实例,并将其添加到目标列表的源数组中 我不能做的是显示目标数组绑定到的Silverlight控件的更新 有什么想法吗 谢谢 private void CheckBox_Checked(object sender, Rout
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>
{
}
private void复选框\u选中(对象发送方,路由目标)
{
var cb=(复选框)发送方;
var children=((网格)cb.Parent).children;
//cb有一个同级TextBlock项,该项的索引位于
//科目表
var ch2=子项[1]作为文本块;
var subjectIndexStr=ch2.Text;
var myWorkingSubject=workingSubjectList[int.Parse(subjectIndexStr)];
开关(myWorkingSubject.SubjectLevelId)
{
//初级科目、中级科目和高级科目的更新
//不要反映在将它们用作资源的列表中。
case(int)SubjectLevels.Elementary:
元素主题。添加(myWorkingSubject);
打破
大小写(int)SubjectLevels.Middle:
middlesubject.Add(myWorkingSubject);
打破
大小写(int)SubjectLevels.High:
highSubjects.Add(我的工作主题);
打破
默认:中断;
}
}
//这就是目标类的声明方式。
公共类主题:可观察集合
{
}
公共类SubjectsMiddle:ObservableCollection
{
}
公共类主题:ObservableCollection
{
}
以下是.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>
我不太确定,但这可以通过在Dispatch.BeginInvoke()中进行更改来解决 您可以将switch语句重构为名为
UpdateListBox
的新方法,然后调用它:
Dispatcher.BeginInvoke(() => UpdateListBox(myWorkingSubject.SubjectLevelId))
可能这是因为XAML正在更新对象的一个新实例,并将数据绑定到该实例 尝试将其添加到Page.xaml.cs(或控件所在的位置)上的协构造函数中 等等 也许这会有帮助。我在几个场合将列表框绑定到可观察的集合,实现了相同的概念,但没有经历过您遇到的情况 我有几个建议: 您在支票兑换活动中尝试过这个吗
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.谢谢您的回复。我不能说我理解Dispatcher.BeginInvoke()除了在队列上启动一个deligate之外做了什么。我在想,只要在绑定的数据收集中添加一个新元素就足够了。UpdateListBox()将如何更新列表框?
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");
}