独自のキャンバスに長方形を描画する「WaferMap」というクラスがあります。メインビューにもキャンバスがあります。WaferMap クラスのキャンバスのコンテンツを GUI 上のキャンバスにバインドできるようにしたいと考えています。これを行う最善の方法は何ですか?
私が現在行っている方法は、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 上のキャンバスのデータコンテキストが設定されます。
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;
}
何らかの理由で、これは機能していません。どんな助けでも大歓迎です。
クラスでキャンバスを作成し、それをビューのキャンバスにバインドしようとする以外に、これを行うべきより良い方法はありますか?
ありがとうございます!