2016-10-22 64 views
1

創建形狀(矩形,橢圓,線)到畫布中。然後我在另一個窗口中有一個列表視圖,我需要輸入形狀信息(比如位置,大小,形狀等)。將形狀信息添加到ListView創建時

我在XAML這個代碼在另一個窗口:

<ListView Name="Information"> 

     <ListView.View> 
      <GridView> 
       <GridViewColumn Header="Type"/> 
       <GridViewColumn Header="PositionX"/> 
       <GridViewColumn Header="PositionY"/> 
       <GridViewColumn Header="Width" DisplayMemberBinding="{Binding ActualWidth}"/> 
       <GridViewColumn Header="Height" DisplayMemberBinding="{Binding ActualHeight}"/> 
      </GridView> 
     </ListView.View> 

    </ListView> 

,並在主窗口的C#中,我有一個觀察的收集和驗證碼:

ObservableCollection<Shape> shapes = new ObservableCollection<Shape>(); 

myRect.Width = var1; 
myRect.Height = var2; 
Page.Children.Add(myRect); 
Canvas.SetLeft(myRect, posx); 
Canvas.SetTop(myRect, posy); 

shapes.Add(myRect); 
2ndwindow.Information.ItemsSource = shapes; // this is working because the 2ndwindow is owned by the mainwindow 

編輯:我設法綁定寬度和高度,但我不知道如何綁定位置和它的形狀(矩形或橢圓形)

+1

參見[這個答案](http://stackoverflow.com/a/22325266/ 1136211)瞭解如何在MVVM應用程序中使用矩形完成此操作。您可以通過聲明不同的形狀類型(類)併爲它們使用不同的DataTemplates來推廣它。 – Clemens

回答

2

在適當的MVVM方法中,您應該有一個具有抽象表示的視圖模型(而不是UI元素列表),例如像這樣:

public class ShapeData 
{ 
    public string Type { get; set; } 
    public Geometry Geometry { get; set; } 
    public Brush Fill { get; set; } 
    public Brush Stroke { get; set; } 
    public double StrokeThickness { get; set; } 
} 

public class ViewModel 
{ 
    public ObservableCollection<ShapeData> Shapes { get; } 
     = new ObservableCollection<ShapeData>(); 
} 

您現在可以將此視圖模型綁定到如下所示的視圖。每個形狀的位置和大小從形狀對象的GeometryBounds屬性中檢索。

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions> 

    <ItemsControl ItemsSource="{Binding Shapes}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <Canvas/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Path Data="{Binding Geometry}" 
         Fill="{Binding Fill}" 
         Stroke="{Binding Stroke}" 
         StrokeThickness="{Binding StrokeThickness}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

    <ListView Grid.Column="1" ItemsSource="{Binding Shapes}"> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn Header="Type" 
           DisplayMemberBinding="{Binding Type}"/> 
       <GridViewColumn Header="X" 
           DisplayMemberBinding="{Binding Geometry.Bounds.X}"/> 
       <GridViewColumn Header="Y" 
           DisplayMemberBinding="{Binding Geometry.Bounds.Y}"/> 
       <GridViewColumn Header="Width" 
           DisplayMemberBinding="{Binding Geometry.Bounds.Width}"/> 
       <GridViewColumn Header="Height" 
           DisplayMemberBinding="{Binding Geometry.Bounds.Height}"/> 
      </GridView> 
     </ListView.View> 
    </ListView> 
</Grid> 

你可以這樣建立在窗口的構造視圖模型的實例,並添加一些樣本數據:

public MainWindow() 
{ 
    InitializeComponent(); 

    var viewModel = new ViewModel(); 

    viewModel.Shapes.Add(new ShapeData 
    { 
     Type = "Circle", 
     Geometry = new EllipseGeometry(new Point(100, 100), 50, 50), 
     Fill = Brushes.Orange, 
     Stroke = Brushes.Navy, 
     StrokeThickness = 2 
    }); 

    viewModel.Shapes.Add(new ShapeData 
    { 
     Type = "Rectangle", 
     Geometry = new RectangleGeometry(new Rect(200, 50, 50, 100)), 
     Fill = Brushes.Yellow, 
     Stroke = Brushes.DarkGreen, 
     StrokeThickness = 2 
    }); 

    DataContext = viewModel; 
}