C# 尝试通过数据库从列表框中删除项目时
system.invalidoperationexception中出错: 位于system.Windows.Media.VisualTreeHelper.GetRelative(dependencyobject 参考,RelativeKind,RelativeKind)在 System.Windows.Media.VisualTreeHelper.GetChildrenCount(DependencyObject 参考)在 这是listbox的xaml代码C# 尝试通过数据库从列表框中删除项目时,c#,C#,system.invalidoperationexception中出错: 位于system.Windows.Media.VisualTreeHelper.GetRelative(dependencyobject 参考,RelativeKind,RelativeKind)在 System.Windows.Media.VisualTreeHelper.GetChildrenCount(DependencyObject 参考)在 这是listbox的xaml代码 <ListBox Hei
<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());
}
}
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);