2012-08-16 12 views
1

여기에서 볼 수 있듯이 나는 잠시 전에 질문이있었습니다 (중복되지 않습니다) : WPF Simple DataMatrix. 화면에 LED 조명 매트릭스를 만드는 방법에 대해 질문했습니다. 나는 그 표식화 된 답을 사용하여 행렬을 만들었다. 그것은 매우 잘 표시되며 타원에 명령을 적용하여 매트릭스를 편집 할 수 있지만 지연 없이도 작업 할 수 있습니다. 내가 LedMatrix라는 클래스를 가지고있는 배경에서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> 

:

는 그 결과이 매트릭스 내 코드입니다. 매트릭스는 다른 컨트롤에 포함되어

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

: 그것은 LED의 컬렉션 속성을 가지고 내가이있는 매트릭스의 ItemsSource를 설정 볼 수 있듯이

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

컨테이너.

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

그리고 나는 이미 전에 보여 LedMatrix합니다 (ObservableCollection<ObservableCollection<Led>>) : Itemssource이 같은 인터페이스가된다.

그리고 지금은 매우 중요한

: -이 애니메이션의 일종이다 매우 자주 있기 때문에 나는 변화에게 LedMatrix (LedGridContainer를 참조 LedGrid의 Itemssource)가 있습니다. 문제는 모두 매우 느리다는 것입니다. 그래서 몇 가지 최적화를 알고 있는지 물어보고 싶었습니다.

다시 LedMatrix를 매우 빠르게 변경해야합니다.

답변

0

매번 완전히 새로운 변경 내용을 Led 변경 사항으로 적용하면 전체 그리드가 반복해서 렌더링되어야합니다. 변경중인 Led의 State 속성을 변경해야합니다 (이 경우 PropertyChanged을 실행해야 함).

또한 Led의 양이 일정하면 ObservableCollection이 전혀 필요하지 않습니다. IEnumerable을 사용할 수 있습니다.

+0

좋아요. 나는 또한 단지 상태를 동기화해야한다는 것을 알았고 성능은 엄청납니다. 그러나 ObservableCollection을 가진 것은 ... led의 양은 일정하지 않으므로 하나를 사용하지 않아도됩니다 ... 당신은 맞습니다만 observablecollection은 성능을 취합니까? –

+0

그래, 필요 없어. ObservableColletion의 장점은 항목을 제거, 추가 또는 순서대로 이동하면 바인딩 된 ItemControl이 수정 된 항목을 다시 렌더링한다는 것입니다. 그러나 귀하의 경우 컬렉션 내의 항목 속성 만 변경되므로이 기능은 사용되지 않습니다. 나중에 컬렉션을 작성하는 동안 성능이 저하 될 수 있습니다. 나중에는 생각하지 않습니다. – DanielB