创建后将形状信息添加到ListView [英] Add Shape information to a ListView when its created

查看:47
本文介绍了创建后将形状信息添加到ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在画布中创建形状(矩形,椭圆形,直线)。然后我在另一个窗口中有一个列表视图,我需要在其中输入形状信息(例如位置,大小,它是什么形状)。

Im creating shapes(rectangles, ellipse, line) in to a canvas. And then i have in another window a listview where i need to input the information of the shape(such like position, size, what shape it is).

我有这个XAML代码在另一个窗口中:

I have this code in XAML in another window:

    <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#中,我有一个可观察的集合,并且此代码为:

and in c# of the main window, i have an observable collection and this code:

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

编辑:我设法绑定了宽度和高度,但是我不知道如何绑定位置和形状(矩形或椭圆形)

i managed to bind the width and height, but i dont know how to bind the position and the shape it is(rectangle or ellipse)

推荐答案

MVVM方法,您应该拥有一个带有Shape的抽象表示(而不是UI元素列表)的视图模型,例如像这样:

In a proper MVVM approach, you should have a view model with an abstract representation of a Shape (instead of a list of UI elements), e.g. like this:

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>();
}

您现在可以将此视图模型绑定到如下所示的视图。从形状对象的 Geometry Bounds 属性检索每个形状的位置和大小。

You could now bind this view model to a view like shown below. The position and size of each shape is retrieved from the Bounds property of the Geometry of a shape object.

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

您可以在窗口的构造函数中创建视图模型实例,并添加一些示例数据,如下所示:

You could create a view model instance in your window's constructor and add some sample data like this:

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

这篇关于创建后将形状信息添加到ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆