我不知道为什么,但当我定义DataGrid的ItemsSource时,它会造成一些内存泄漏。
这是我的DataGrid的xaml代码:
<DataGrid x:Name="DataGrid1" AutoGenerateColumns="True" IsReadOnly="True" ClipboardCopyMode="ExcludeHeader" Margin="20,250,20,20" SelectionUnit="Cell" Style="{DynamicResource MaterialDesignDataGrid}" SelectionMode="Single"/>
这是我定义ItemsSource的代码:
cmd = New SqlCommand With {
.CommandText = strsql,
.Connection = DBConn.ADONETconn
}
da = New SqlDataAdapter(cmd)
dt = New DataTable("RECH")
da.Fill(dt)
DataGrid1.ItemsSource = dt.DefaultView
我知道这是由行DataGrid1引起的。dt.DefaultView
,因为如果我注释它,内存泄漏不会发生。
如果你有任何线索,请与我分享
找到了!实际上,它来自于我的DataGrid在StackPanel中的事实。我仍然不知道为什么,但这是个问题。