我有一個名爲「WaferMap」的類,它繪製矩形自己的畫布。在主視圖中也有一個畫布。我希望能夠將WaferMap類中的畫布內容綁定到GUI上的畫布。做這個的最好方式是什麼?將畫布綁定到WPF中的畫布
我現在要做的方式是在WaferMap類中創建一個畫布,然後使用畫布的ContentPresenter屬性將它綁定到GUI上的畫布。
晶片圖類:
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上設置畫布的datacontext。
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;
}
由於某種原因,這是行不通的。任何幫助將不勝感激。
除了在類中創建畫布並試圖將其綁定到視圖中的畫布以外,還有更好的方法嗎?
感謝先進!
也許你的綁定應該在WaferMapCanvas屬性上......看起來你錯誤地使用了類名稱WaferMap。 – 2013-02-14 13:28:26