Note that there are some explanatory texts on larger screens.

plurals
  1. POMouse events not firing when databinding to a Silverlight ContentControl
    primarykey
    data
    text
    <p>I am in the process of taking a Silverlight 4 UserControl containing a canvas which has a number of FrameworkElements on it and converting this to use databinding.</p> <p>The XAML for my original canvas was:</p> <pre><code>&lt;Canvas x:Name="panelDisplay" &gt; &lt;Rectangle Width="50" Height="50" MouseLeftButtonDown="Element_MouseLeftButtonDown" Stroke="Aqua" StrokeThickness="5" Fill="Aquamarine" Canvas.Left="450" Canvas.Top="50" x:Name="rect1" /&gt; &lt;Image Source="../Images/3.jpg" Stretch="UniformToFill" Width="356" Height="224" MouseLeftButtonDown="Element_MouseLeftButtonDown" Canvas.Left="317" Canvas.Top="140" x:Name="image1" /&gt; &lt;/Canvas&gt; </code></pre> <p>This displays the rectangle and image and the MouseLeftButtonDown event fires which then deals with operations such as dragging and resizing.</p> <p>In order to get this working with databinding I created an object called CanvasElement:</p> <pre><code>public class CanvasElement { public CanvasElement(int id, object elementContent, double width, double height, int left, int top) { Id = id; ElementContent = elementContent; Width = width; Height = height; Left = left; Top = top; } public int Id { get; set; } public object ElementContent { get; set; } public double Width { get; set; } public double Height { get; set; } public int Left { get; set; } public int Top { get; set; } } </code></pre> <p>The ElementContent is used to store the Rectangle or Image. I populate an ObservableCollection called CanvasElements and assign the DataContext of the control. I have changed my XAML to:</p> <pre><code>&lt;Canvas x:Name="panelDisplay" &gt; &lt;ItemsControl x:Name="CanvasElements" ItemsSource="{Binding Path=CanvasElements}"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Canvas&gt; &lt;ContentControl Content="{Binding Path=ElementContent}" Height="{Binding Path=Height}" Width="{Binding Path=Width}" Canvas.Left="{Binding Path=Left}" Canvas.Top="{Binding Path=Top}" MouseLeftButtonDown="CanvasElement_MouseLeftButtonDown" /&gt; &lt;/Canvas&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/Canvas&gt; </code></pre> <p>My elements appear but the CanvasElement_MouseLeftButtonDown does not fire. I have also used ContentControl within the ItemsControl.</p> <p>My questions are:</p> <p>Is this is sensible way to go?</p> <p>Why is MouseLeftButtonDown not firing?</p>
    singulars
    1. This table or related slice is empty.
    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