2013-04-29 2 views

답변

-1

아니요, 더 좋은 방법은 없습니다.

XAML에서 모양을 정의하면 모양이 Canvas.Children에 추가()됩니다.

이제 Mvvm 방식으로 깨끗하게 만들고 싶다면 viewmodel으로 창의력을 발휘해야 할 것입니다. 핸들러에서 ObservableCollection에 어떤 종류의 객체를 추가 한 다음 뷰에서 무언가를 수행하여 ViewModel Collection에서 모양을 만들면 버튼에 대한 VM ICommand를 추가 할 수 있습니다. (xaml 또는 코드 숨김)

5

ItemsControlCanvas을 항목 패널로 사용할 수 있습니다. 그런 다음 VM에서 모든 항목을 보유 할 컬렉션이 필요합니다. 각 항목에는 배치를위한 모든 특성이 있어야합니다.

항목 : 다음 VM에서

public class CanvasShape : INotifyPropertyChanged 
{ 
    public double Top {get; set;}//TODO: add change notification 
    public double Left {get; set;}//TODO: add change notification 
    public Geometry PathData {get; set;}//TODO: add change notification 
} 

:

public ObservableCollection<CanvasShape> Shapes {get; set;} 
..... 
//Add some logic to fill the collection 

그래서, 코드, 그것은 (나는 간결에 대한 변경 알림을 생략 해요)과 같이 표시됩니다 XAML의 경우 :

<!--The DataContext here is the VM--> 
<ItemsControl ItemsSource="{Binding Shapes}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas IsItemsHost="True"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemContainerStyle> 
     <Style> 
      <!--These setters will control the position of the shape; the DataContext here is CanvasShape--> 
      <Setter Property="Cavas.Top" Value="{Binding Top}"/> 
      <Setter Property="Cavas.Left" Value="{Binding Left}"/> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Path Data="{Binding PathData}" 
        ....... 
        /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
관련 문제