我使用项目模板在WF中创建了一个已检查的ListBox。 这是xaml代码。
<ListBox.ItemTemplate>
<DataTemplate x:Name="datatemplate_List">
<StackPanel x:Name="stackpanel_List" Orientation="Horizontal" >
<CheckBox x:Name="checkbox_List" VerticalAlignment="Center" HorizontalAlignment="Left" Width="22"/>
<Label x:Name="label_List" Height="18" FontSize="12" Padding="0" VerticalContentAlignment="Top" Content="{TemplateBinding ContentPresenter.Content}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
我还有另一个usercontrol,里面有这个已检查的列表框。此usercontrol具有一个属性,用于获取和设置已选中列表框的复选框。 get函数返回所有已检查项的列表,并且此函数正确执行,set函数取消选中列表框中的所有项,然后仅检查值中检查的项。
public List<string> measurements
{
get
{
List<string> measList = new List<string>();
//foreach (string chkBox in chkListBox_Meas.CheckedItems) measList.Add(chkBox); marks code
for (int i = 0; i < chkListBox_Meas.Items.Count; i++)
{
ListBoxItem listBoxItem = (ListBoxItem)(chkListBox_Meas.ItemContainerGenerator.ContainerFromItem(chkListBox_Meas.Items[i]));
ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
DataTemplate dataTemplate = contentPresenter.ContentTemplate;
StackPanel stack = (StackPanel)dataTemplate.FindName("stackpanel_List", contentPresenter);
CheckBox checkBox = (CheckBox)stack.FindName("checkbox_List");
Label label = (Label)stack.FindName("label_List");
if (checkBox.IsChecked == true)
{
measList.Add(label.Content.ToString());
}
}
return measList;
}
set
{
for (int i = 0; i < chkListBox_Meas.Items.Count; i++)
{
ListBoxItem listBoxItem = (ListBoxItem)chkListBox_Meas.ItemContainerGenerator.ContainerFromItem(chkListBox_Meas.Items[i]);
ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
DataTemplate dataTemplate = contentPresenter.ContentTemplate;
StackPanel stack = (StackPanel)dataTemplate.FindName("stackpanel_List", contentPresenter);
CheckBox checkBox = (CheckBox)stack.FindName("checkbox_List");
checkBox.IsChecked = false;
}
foreach (string meas in value)
{
for (int i = 0; i < chkListBox_Meas.Items.Count; i++)
{
ListBoxItem listBoxItem = (ListBoxItem)(chkListBox_Meas.ItemContainerGenerator.ContainerFromItem(chkListBox_Meas.Items[i]));
ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
DataTemplate dataTemplate = contentPresenter.ContentTemplate;
StackPanel stack = (StackPanel)dataTemplate.FindName("stackpanel_List", contentPresenter);
CheckBox checkBox = (CheckBox)stack.FindName("checkbox_List");
Label label = (Label)stack.FindName("label_List");
if (label.Content.ToString() == meas) checkBox.IsChecked = true;
}
}
}
}
执行此行代码时出现问题。
ListBoxItem listBoxItem = (ListBoxItem chkListBox_Meas.ItemContainerGenerator.ContainerFromItem(chkListBox_Meas.Items[i]);
当在get函数中执行它时,它返回一个listboxitem,如它所假设的那样。但是当在set函数中执行此行时,它返回null。
答案 0 :(得分:0)
null
返回ItemContainerGenerator
值的原因是ListBoxItem listBoxItem = (ListBoxItem)(chkListBox_Meas.ItemContainerGenerator.ContainerFromItem(chkListBox_Meas.Items[i]));
行
ItemContainerGenerator.StatusChanged
尚未生成尝试访问权限。
解决方案是调用ContainerGenerated
事件处理程序并等待状态更改为ListBoxItem
。一旦发生这种情况,您将能够访问Me.Subform.Requery