WPF数据绑定非常慢

时间:2012-08-16 15:02:18

标签: c# wpf xaml data-binding

我前段时间有一个问题(这是没有重复的),你可以在这里看到:WPF Simple DataMatrix。 我问到在屏幕上创建一个LED灯矩阵。我曾经使用过标记的答案并创建了矩阵。它显示得非常好,我还在Ellipse上应用了命令,因此我可以编辑矩阵,但也可以没有任何延迟。

结果这是我的Matrix代码:

<ItemsControl x:Class="HTLED.WPF.Controls.LedGrid"
         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" 
         xmlns:Data="clr-namespace:HTLED.Data;assembly=HTLED.Data"
         xmlns:Controls="clr-namespace:HTLED.WPF.Controls"
         xmlns:Commands="clr-namespace:HTLED.Client.Commands;assembly=HTLED.Client"
         xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:ViewModel="clr-namespace:HTLED.Client.ViewModel;assembly=HTLED.Client"
         mc:Ignorable="d"
         d:DataContext="{d:DesignInstance ViewModel:LedContainerViewModel}" Name="ledGridRoot" >
<ItemsControl.Resources>
    <DataTemplate x:Key="ledTemplate" DataType="{x:Type Data:Led}">
        <Ellipse Name="ellipse" Fill="Green" Stretch="Uniform" SnapsToDevicePixels="True">
            <Interactivity:Interaction.Triggers>
                <Interactivity:EventTrigger EventName="PreviewMouseMove">
                    <Commands:CommandTrigger Command="{Binding ElementName=ledGridRoot, Path=DataContext.LedGridViewModel.LedMouseMoveCommand}" PassEventArgsToCommand="True"/>
                </Interactivity:EventTrigger>
                <Interactivity:EventTrigger EventName="PreviewMouseLeftButtonDown">
                    <Commands:CommandTrigger Command="{Binding ElementName=ledGridRoot, Path=DataContext.LedGridViewModel.LedOnCommand}" PassEventArgsToCommand="True"/>
                </Interactivity:EventTrigger>
                <Interactivity:EventTrigger EventName="PreviewMouseRightButtonDown">
                    <Commands:CommandTrigger Command="{Binding ElementName=ledGridRoot, Path=DataContext.LedGridViewModel.LedOffCommand}" PassEventArgsToCommand="True"/>
                </Interactivity:EventTrigger>
            </Interactivity:Interaction.Triggers>
        </Ellipse>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=State}" Value="Off">
                <Setter TargetName="ellipse" Property="Fill" Value="Red"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource ledTemplate}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Controls:StretchStackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

在后台我有一个名为LedMatrix的课程。它有一个收集LED的属性:

    ObservableCollection<ObservableCollection<Led>> _leds;
    public ObservableCollection<ObservableCollection<Led>> Leds
    {
        get { return _leds ?? (_leds = CreateMatrix(XSize, YSize)); }
        set { SetProperty(value, ref _leds, () => Leds); }
    }

矩阵由另一个控件包含:

<Canvas x:Class="HTLED.WPF.Controls.LedContainer"
         ....
         mc:Ignorable="d" 
         d:DataContext="{d:DesignInstance ViewModel:LedContainerViewModel}"
         d:DesignHeight="300" d:DesignWidth="300" Name="layoutRoot" Drawing:DrawingCore.EnableDrawing="True">
<Viewbox Canvas.Top="0" Canvas.Left="0" Width="{Binding ElementName=layoutRoot, Path=ActualWidth}"
         Height="{Binding ElementName=layoutRoot, Path=ActualHeight}">
    <Grid>
        <Controls:LedGrid Width="50000" Height="25000" Margin="500" DataContext="{Binding Path=Main.LedContainerViewModel}" ItemsSource="{Binding Path=LedContentContainer.Content}" />
    </Grid>
</Viewbox>

如您所见,我在容器中设置了Matrix的ItemsSource。 Itemssource是这样的接口:

public interface ILedContentContainer
{
    LedMatrix Content { get; set; }
}

我之前已经展示了LedMatrix(ObservableCollection<ObservableCollection<Led>>)。

现在非常重要: 我经常更改LedMatrix(LedGrid的Itemssource - 请参阅LedGridContainer)因为它是某种动画。问题在于一切都非常缓慢。所以我想问你是否知道一些最佳选择?

我必须非常快速地改变LedMatrix。

1 个答案:

答案 0 :(得分:0)

如果每次Led更改时都应用全新的ObservableCollection,则必须一次又一次地渲染完整的网格。您应该更改正在更改的Led的State属性(它们应该触发PropertyChanged)。

此外,如果Led的数量不变,则根本不需要ObservableCollection。您可以使用任何IEnumerable