wpf - 如何提高 wpf 用户控制性能?

标签 wpf performance mvvm user-controls datatemplate

我有一个用户控件,当我在一个窗口中有很多用户控件时,加载需要很长时间。
如果我将其更改为自定义控件或 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)DataGridItemSource给您的DataCollection.Source .

关于wpf - 如何提高 wpf 用户控制性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10974437/

相关文章:

c# - 在 WPF 中创建忙碌动画的最简单方法

jquery - asp.net 网站性能 - 1 个页面缓慢会影响同一浏览器 session 上的其余页面

c# - 使用 StaticResource 中的 ItemsSource 将命令绑定(bind)到 Viewmodel

wpf - 使用 Storyboard的动态动画

c# - Caliburn 微 : Disable button on form validation error

C# WPF 禁用退出/关闭按钮

javascript - 我可以优化此 js 原型(prototype)库以在 IE 中更快地制作动画吗?

google-chrome - 为什么我的 Google Chrome 扩展程序的弹出 UI 在外接显示器上滞后,而在笔记本电脑的 native 屏幕上却没有?

wpf - MVVM中的动画图像

c# - 是否有可能以 mvvm 模式在 wpf datagrid 上获取动态列?