1

我有一个名为“WaferMap”的类,它在自己的画布上绘制矩形。主视图中也有一个画布。我希望能够将 WaferMap 类中画布的内容绑定到 GUI 上的画布。做这个的最好方式是什么?

我现在的做法是在 WaferMap 类中创建一个画布,然后使用画布的 ContentPresenter 属性将其绑定到 GUI 上的画布。

WaferMap 类:

public class WaferMap : ViewModelBase
{
#region Fields

    protected const int rows = 23;
    protected const int cols = 23;
    protected int currentRow;
    protected int currentCol;
    protected DiePrint diePrint;
    protected List<BlueTape> blueTapeList;
    protected Canvas canvasToMap;

#endregion

#region Constructor

    public WaferMap(List<BlueTape> btList)
    {
        blueTapeList = new List<BlueTape>();
        blueTapeList = btList;
    }

#endregion

#region Properties

    public Canvas WaferMapCanvas
    {
        get
        {
            return canvasToMap;
        }
    }
    public List<BlueTape> BlueTapeList
    {
        get
        {
            return blueTapeList;
        }
    }

#endregion

#region Methods

    public void DrawWaferMap()
    {
        if (blueTapeList.Count > 0)
        {
            canvasToMap = new Canvas();
            foreach (BlueTape bt in blueTapeList)
            {
                if (bt.DiePrintList.Count > 0)
                {
                    foreach (DiePrint print in bt.DiePrintList)
                    {
                        // Create a new DiePrintRectangle and get its
                        // row and column coordinates
                        DiePrintRectangle diePrintRect = new DiePrintRectangle(print);

                        // Add the print to the canvas
                        canvasToMap.Children.Add(diePrintRect);

                        // Set the properties
                        Thickness margin = new Thickness(0);
                        diePrintRect.Margin = margin;
                        diePrintRect.Height = 25;
                        diePrintRect.Width = diePrintRect.Height;
                        diePrintRect.HorizontalAlignment = HorizontalAlignment.Left;
                        diePrintRect.VerticalAlignment = VerticalAlignment.Top;
                        currentCol = Convert.ToInt32(print.Col * diePrintRect.Height);
                        currentRow = Convert.ToInt32(print.Row * diePrintRect.Height);
                        print.MapCol = currentCol;
                        print.MapRow = currentRow;
                        Canvas.SetLeft(diePrintRect, currentCol);
                        Canvas.SetTop(diePrintRect, currentRow);

                        // Get the color of the print fill and stroke
                        //diePrintRect.Stroke = GetDiePrintColor(bt);
                        diePrintRect.StrokeThickness = 12;
                        diePrintRect.Stroke = GetDiePrintColor(bt);
                        diePrintRect.Fill = Brushes.Transparent;
                        diePrintRect.MouseDown += diePrintRect_MouseDown;
                        diePrintRect.MouseEnter += diePrintRect_MouseEnter;
                    }
                }
            }
        }
    }
}

这是我的 XAML:

    <DockPanel>
        <Canvas Name="mapCanvas">
            <ContentPresenter Content="{Binding WaferMap}"/>
        </Canvas>
    </DockPanel> 

在后面的代码中:

注意:我有这个用于调试绑定的设置。单击“btnDoStuff”按钮时,它将在 GUI 上设置画布的数据上下文。

public partial class WaferTrackerWindow : Window
{
    WaferTrackerWindowViewModel wtw;
    public WaferTrackerWindow()
    {
        InitializeComponent();

        SelectWaferButtonViewModel swbv = new SelectWaferButtonViewModel();
        wtw = new WaferTrackerWindowViewModel(tvwWaferList, txtFilter);            

        wtw.SelectWaferButtonViewModel = swbv;

        tvwDockPanel.DataContext = wtw.SelectWaferButtonViewModel;
        DataContext = wtw;
        btnChooseWafer.DataContext = wtw;
        btnSelectWafer.DataContext = wtw;
        btnExit.DataContext = wtw;
        tbkWafer.DataContext = wtw;

        btnDoStuff.Click += btnDoStuff_Click;
    }

    private void btnDoStuff_Click(object sender, RoutedEventArgs e)
    {
        mapCanvas.DataContext = wtw.WaferMap.WaferMapCanvas;
    }

由于某种原因,这不起作用。任何帮助将不胜感激。

除了在类中创建画布并尝试将其绑定到视图中的画布之外,还有更好的方法吗?

提前致谢!

4

1 回答 1

5

您正在设置您DataContextmapCanvasto wtw.WaferMap.WaferMapCanvas,这是一个类型的对象Canvas,并且Canvas没有名为WaferMap

您需要改为设置DataContexttowtf.WaferMap并绑定到WaferMapCanvas数据类型的属性Canvas

mapCanvas.DataContext = wtw.WaferMap;

<ContentPresenter Content="{Binding WaferMapCanvas}"/>
于 2013-02-14T13:35:24.420 回答