提问人:rukiman 提问时间:11/14/2023 最后编辑:Sir Ruforukiman 更新时间:11/14/2023 访问量:77
在 ItemsControl 中使用 TextBox 时发生内存泄漏
Memory leak when using a TextBox within an ItemsControl
问:
我一直在努力寻找内存泄漏的原因。我有以下 XAML:
<ItemsControl Grid.Row="0" Grid.Column="1" x:Name="FieldsView" Background="Transparent">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0 5">
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="300" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1">
<Grid>
<TextBox Tag="firstname" Text="" />
</Grid>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我删除了所有绑定以简化 XAML。我正在注释一些东西以找到核心泄漏。
根据 dotMemory 的说法,这是泄漏分析
我知道它是文本框,因为如果我注释掉它,泄漏就会消失。我正在使用 .NET Framework 4.7.2。
我看到一篇关于撤消功能的帖子,所以我设置了我的文本框的样式以删除它,但这并没有解决它。
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsUndoEnabled" Value="False"/>
<Setter Property="Background" Value="{StaticResource TextBox.Background}" />
<Setter Property="Opacity" Value="60" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FontSize" Value="22pt"/>
<Setter Property="FontFamily" Value="Segoe UI Light" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="#FF7EB4EA" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ItemsControl 是使用 ObservableCollection 填充的
private ObservableCollection<RegistrationFieldViewModel> _model;
_model = new ObservableCollection<RegistrationFieldViewModel>(visitorTypeSettingInfo.Fields.Select(a => new RegistrationFieldViewModel
{
Key = a.Key,
Mandatory = a.Mandatory,
UseAsLookup = a.UseAsLookup,
Type = a.Type
}).ToList());
FieldsView.ItemsSource = _model;
和 RegistrationFieldViewModel 实现 INotifyPropertyChanged
答: 暂无答案
评论
TextBox
Label
TextBlock