wpf - Datagrid 绑定(bind)文本框内存泄漏

标签 wpf .net-4.0 datagrid

我翻了很多关于数据网格泄漏的文章,但找不到正确的方法。 这是一些要重现的代码。它有数据网格和按钮,每次按下按钮,它都会用随机数填充表格,并占用 10mb 内存,并且不会释放它。 绑定(bind)到 TextBlock 不会导致内存泄漏,但我需要文本框双向绑定(bind)。

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <DataGrid Name="dg" Grid.Row="0" AutoGenerateColumns="False" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text0}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text1}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text2}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text3}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text4}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text5}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text6}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text7}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text8}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text9}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="update" Grid.Row="1" Width="Auto" Height="Auto" Click="Button_Click"></Button>
</Grid>

namespace test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();


    }

    public ObservableCollection<obj> res;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        res = new ObservableCollection<obj>();


        for (var i = 0; i < 20; i++)
        {
            res.Add(new obj
            {
                Text0 = ((new Random()).NextDouble() * 100).ToString(),
                Text1 = ((new Random()).NextDouble() * 100).ToString(),
                Text2 = ((new Random()).NextDouble() * 100).ToString(),
                Text3 = ((new Random()).NextDouble() * 100).ToString(),
                Text4 = ((new Random()).NextDouble() * 100).ToString(),
                Text5 = ((new Random()).NextDouble() * 100).ToString(),
                Text6 = ((new Random()).NextDouble() * 100).ToString(),
                Text7 = ((new Random()).NextDouble() * 100).ToString(),
                Text8 = ((new Random()).NextDouble() * 100).ToString(),
                Text9 = ((new Random()).NextDouble() * 100).ToString(),
            });
        }
        dg.DataContext = res;
    }


}
public class obj : INotifyPropertyChanged
{
    private string text0;
    public string Text0
    {
        get
        {
            return text0;
        }
        set
        {
            text0 = value;
            NotifyPropertyChanged("Text0");
        }
    }
    private string text1;
    public string Text1
    {
        get
        {
            return text1;
        }
        set
        {
            text1 = value;
            NotifyPropertyChanged("Text1");
        }
    }
    private string text2;
    public string Text2
    {
        get
        {
            return text2;
        }
        set
        {
            text2 = value;
            NotifyPropertyChanged("Text2");
        }
    }
    private string text3;
    public string Text3
    {
        get
        {
            return text3;
        }
        set
        {
            text3 = value;
            NotifyPropertyChanged("Text3");
        }
    }
    private string text4;
    public string Text4
    {
        get
        {
            return text4;
        }
        set
        {
            text4 = value;
            NotifyPropertyChanged("Text4");
        }
    }
    private string text5;
    public string Text5
    {
        get
        {
            return text5;
        }
        set
        {
            text5 = value;
            NotifyPropertyChanged("Text5");
        }
    }
    private string text6;
    public string Text6
    {
        get
        {
            return text6;
        }
        set
        {
            text6 = value;
            NotifyPropertyChanged("Text6");
        }
    }
    private string text7;
    public string Text7
    {
        get
        {
            return text7;
        }
        set
        {
            text7 = value;
            NotifyPropertyChanged("Text7");
        }
    }
    private string text8;
    public string Text8
    {
        get
        {
            return text8;
        }
        set
        {
            text8 = value;
            NotifyPropertyChanged("Text8");
        }
    }
    private string text9;
    public string Text9
    {
        get
        {
            return text9;
        }
        set
        {
            text9 = value;
            NotifyPropertyChanged("Text9");
        }
    }

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

最佳答案

如果您可以使用 DataGridTextColumn 而不是 DataGridTemplateColumn,您的内存就不会泄漏。

不知道为什么 DataGridTemplateColumn 有内存泄漏

编辑:

我也会只初始化一次 OberservableColletion,只设置一次数据上下文并在 button_click 中使用 clear。

编辑:DataGridTemplateColumn 的内存泄漏问题似乎在 .NET4.5 中消失了

关于wpf - Datagrid 绑定(bind)文本框内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13081064/

相关文章:

c# - 选择/拾取在 Direct2D 中应该如何工作?

c# - 对可能未初始化的字典元素执行加等于操作的简洁方法

Silverlight、Caliburn、Actions 和 DataGrids

mysql - 如何使用 mysql 即可编辑数据网格在 wpf 数据网格本身中插入更新删除

ruby-on-rails-3 - Ruby on Rails - 在 Wice 网格中显示高级搜索结果

c# - 舍入 WPF 绑定(bind)中的值

c# - WPF:使用 GridSplitter 隐藏网格行

.net - 确保每个子 UserControl 创建自己的 ViewModel 实例

c# - 将属性绑定(bind)到依赖属性时,触发器不起作用

c# - 使用 WorkflowServiceHost 托管工作流