Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding a Canvas to a Canvas in WPF
    primarykey
    data
    text
    <p>I have a class called "WaferMap" that draws rectangles its own canvas. There is also a canvas in the main view as well. I want to be able to bind the content of the canvas in the WaferMap class to the canvas on the GUI. What is the best way to do this?</p> <p>The way I am doing it now is creating a canvas in the WaferMap class and then binding it to the canvas on the GUI using the ContentPresenter property of the canvas.</p> <p><strong>WaferMap class:</strong></p> <pre><code>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&lt;BlueTape&gt; blueTapeList; protected Canvas canvasToMap; #endregion #region Constructor public WaferMap(List&lt;BlueTape&gt; btList) { blueTapeList = new List&lt;BlueTape&gt;(); blueTapeList = btList; } #endregion #region Properties public Canvas WaferMapCanvas { get { return canvasToMap; } } public List&lt;BlueTape&gt; BlueTapeList { get { return blueTapeList; } } #endregion #region Methods public void DrawWaferMap() { if (blueTapeList.Count &gt; 0) { canvasToMap = new Canvas(); foreach (BlueTape bt in blueTapeList) { if (bt.DiePrintList.Count &gt; 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; } } } } } } </code></pre> <p>And here is my XAML:</p> <pre><code> &lt;DockPanel&gt; &lt;Canvas Name="mapCanvas"&gt; &lt;ContentPresenter Content="{Binding WaferMap}"/&gt; &lt;/Canvas&gt; &lt;/DockPanel&gt; </code></pre> <p><strong>And in the code behind:</strong></p> <p><em>NOTE</em>: I have this setup for debugging the binding. When the button "btnDoStuff" gets clicked, it will set the datacontext of the canvas on the GUI.</p> <pre><code>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; } </code></pre> <p>For some reason, this is not working. Any help would greatly appreciated. </p> <p>Is there a better way I should be doing this other than creating a canvas in a class and trying to bind it to a canvas in the view? </p> <p>Thanks in advanced!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload