我有一个用户控件,当我在一个窗口中有很多用户控件时,加载需要很长时间。
如果我将其更改为自定义控件或 DataTemplate
会变得更好吗?有一个类和附加属性?
任何想法将不胜感激。
已编辑:
这是我的控制:
<UserControl
x:Class="Pouyansoft.WPF.MVVM.Control.Common.View.DataGridSelectorControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Name="dataGridSelector"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d" >
<UserControl.Resources>
<CollectionViewSource Source="{Binding DataCollection.Source}" x:Key="theSource"/>
<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid VerticalAlignment="Center" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{TemplateBinding Content}"
HorizontalAlignment="Center" />
<TextBox x:Name="txtSearch" Grid.Row="1" HorizontalAlignment="Stretch"
BorderThickness="1" TextChanged="TextBox_TextChanged" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<DataGrid x:Name="grd"
ItemsSource="{Binding Source={StaticResource theSource}}"
AutoGenerateColumns="False"
ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"
PreviewKeyDown="grd_PreviewKeyDown"
SelectedIndex="{Binding SelectedIndex}"
behavior:MouseDoubleClick.Command="{Binding MouseDoubleClickCommand}"
PreviewMouseLeftButtonUp="grid1_PreviewMouseLeftButtonUp"
GridLinesVisibility="Vertical">
</DataGrid>
</Grid>
以及后面代码中的一些代码。(实际上所有其他控件都具有相同的行为)
最佳答案
首先,不要使用 DynamicResource
使用 StaticResource
-
采用
ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle1}"
代替
ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"
第二件事是检查输出窗口中的绑定(bind)错误,尽可能多地尝试修复。
另外,我看不到使用
CollectionViewSource
的任何好处。 (因为您没有进行任何排序、过滤、分组);如果没有必要使用 CollectionViewSource
,可以直接绑定(bind)DataGrid
的ItemSource
给您的DataCollection.Source
.
关于wpf - 如何提高 wpf 用户控制性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10974437/