2012-08-16 128 views
1

我前一個問題(這是沒有重複),你可以看到這裏:WPF Simple DataMatrix。 我問過在屏幕上創建一個LED燈矩陣。我習慣那個標記的答案並創建矩陣。它顯示得非常好,我也在橢圓上應用命令,所以我可以編輯矩陣,但也可以沒有任何延遲地工作。WPF數據綁定很慢

至於結果,這是我對矩陣代碼:

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

正如你可以看到我設置的矩陣的的ItemsSource在容器。這是的ItemsSource像這樣的接口:

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

而且LedMatrix我之前就已經表明(在ObservableCollection<ObservableCollection<Led>>)。

現在很重要的: 我有變化LedMatrix(LedGrid的的ItemsSource - 見LedGridContainer)很多時候,因爲它是某種形式的動畫。問題是這一切都非常緩慢。所以我想問你是否知道一些優點?

再次,我必須非常快速地更改LedMatrix。

回答

0

如果每次更改全新的ObservableCollection,則LED必須一次又一次地渲染。你應該改變正在改變的Led的State屬性(他們應該激活PropertyChanged)。

此外,如果LED的數量不變,則根本不需要ObservableCollection。您可以使用任何IEnumerable

+0

好吧,我剛剛還發現,我只需要同步狀態,性能變得巨大。但與ObservableCollection ... LED的數量ist恆定,所以我不必使用一個......你是對的,但是一個observablecollection是否需要性能? – 2012-08-16 15:21:00

+0

是的,你不需要它。 ObservableColletion的優點是,項目被刪除,添加或移動順序,綁定的ItemControl再次呈現修改的項目。但在你的情況下,只有集合中的項目屬性發生變化,所以不使用此功能。這可能會降低成本,但是我不認爲。 – DanielB 2012-08-16 15:23:54