1

I have a usercontrol, and when I have lots of that in a window, it takes long time to get loaded. Will it get better if I change it to a customcontrol or maybe a DataTemplate with a class and attached properties? any ideas would be greatly appreciated.

Edited:

this is my control:

<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>

and some code in the code behind.(and actually all the other control has the same behavior)

4

1 回答 1

1

首先,不要使用DynamicResourceuse StaticResource-

利用

ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle1}"

代替

ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"

第二件事是检查输出窗口中的绑定错误,尽可能多地尝试修复。

另外,我没有看到使用的任何好处CollectionViewSource(因为您没有进行任何排序、过滤、分组);如果不需要使用CollectionViewSource,可以直接将DataGrid's绑定ItemSource到你的DataCollection.Source.

于 2012-06-11T07:08:56.087 回答