Skip to main content
 首页 » 编程设计

c#之在 WPF 中将 Canvas 绑定(bind)到 Canvas

2024年02月20日12cyq1162

我有一个名为“WaferMap”的类,它在自己的 Canvas 上绘制矩形。主视图中也有一个 Canvas 。我希望能够将 WaferMap 类中 Canvas 的内容绑定(bind)到 GUI 上的 Canvas 。执行此操作的最佳方法是什么?

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

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>  

在后面的代码中:

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

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

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

除了在类中创建 Canvas 并尝试将其绑定(bind)到 View 中的 Canvas 之外,还有其他更好的方法吗?

提前致谢!

请您参考如下方法:

您正在将 mapCanvas 的 DataContext 设置为 wtw.WaferMap.WaferMapCanvas,它是 Canvas< 类型的对象,而 Canvas 没有名为 WaferMap

的属性

您需要将 DataContext 设置为 wtf.WaferMap 并绑定(bind)到数据类型为 Canvas 的 WaferMapCanvas 属性

mapCanvas.DataContext = wtw.WaferMap; 

和

<ContentPresenter Content="{Binding WaferMapCanvas}"/>