尝试通过数据库从列表框中删除项目时

时间:2014-10-13 11:51:46

标签: c#

  

system.invalidoperationexception错误:             在system.Windows.Media.VisualTreeHelper.GetRelative(dependencyobject   参考,RelativeKind relativekind)At   System.Windows.Media.VisualTreeHelper.GetChildrenCount(DependencyObject的   参考)........................................

这是列表框的xaml代码

    <ListBox Height="617" x:Name="lstStudentname" SelectionChanged="lstStudentname_SelectionChanged" Canvas.Top="89" Width="480">
                 <ListBox.ItemTemplate>
                                <DataTemplate>
                                       <StackPanel Orientation="Horizontal" Height="70" Width="480">
                                        <Image Source="/project;component/delete-128.png" Width="60" Name="deleteitem" Tap="deleteitem_Tap" Height="45" />
                                       <TextBlock Text="{Binding Title}" Width="400" Height="60" x:Name="txtBlkExtra" Foreground="#FF395A95"  FontSize="30"  />
                                       <TextBlock Text="{Binding Linksweb}" Width="400" Height="60" x:Name="linkedline"  Tag="{Binding txtBlkExtra}" Foreground="#FF395A95"  FontSize="19" />
                                     </StackPanel>
                                </DataTemplate>
                 </ListBox.ItemTemplate>
   </ListBox>

这是我从列表框中删除项目的来源

 private void deleteitem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        try
        {
            var listBoxItem = lstStudentname.ItemContainerGenerator.ContainerFromIndex(lstStudentname.SelectedIndex) as ListBoxItem;
            var txtBlk = FindVisualChildByType<TextBlock>(listBoxItem, "txtBlkExtra");
            a = txtBlk.Text;
            using (LinkDataContext StudentDB = new LinkDataContext(strConnectionString))
            {
                var remove = (from r in StudentDB.GetTable<LinkInfo>()
                              where r.Title == a.ToString()
                              select r).FirstOrDefault();

                if (remove != null)
                {
                    StudentDB.GetTable<LinkInfo>().DeleteOnSubmit(remove);
                    StudentDB.SubmitChanges();
                    MessageBox.Show("Delete The Data");
                    var c = from b in StudentDB.GetTable<LinkInfo>() select b.Title;
                     //lstStudentname.ItemsSource = a;
                    List<LinkInfo> dataSource = new List<LinkInfo>();
                    foreach (var x in c)
                    {
                      dataSource.Add(new LinkInfo() { Title = x });
                      }
                    this.lstStudentname.ItemsSource = dataSource;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

并在visualtreehelper的帮助下

    T FindVisualChildByType<T>(DependencyObject element, String name) where T : class
    {
        if (element is T && (element as FrameworkElement).Name == name)
            return element as T;
        int childcount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < childcount; i++)
        {
            T childElement = FindVisualChildByType<T>     (VisualTreeHelper.GetChild(element, i), name);
            if (childElement != null)
                return childElement;
        }
        return null;
    }

之前有效,但突然看到了错误。

时抛出错误
int childcount = VisualTreeHelper.GetChildrenCount(element);
  

引用不是有效的可视化DependencyObject

希望有人能解决问题

0 个答案:

没有答案