Note that there are some explanatory texts on larger screens.

plurals
  1. POXaml name scope
    text
    copied!<p>I need to change Rectangle attribute from C# (his RotateTransform angle)</p> <p>The problem is the rectangle was declared in XAML, and in the C# code is out of scope for the rectangle, I tried to use Name, and X:Name without succeed,</p> <p>How should I do it?</p> <p>-------------Edit--------------------</p> <pre><code> &lt;ContentControl Width="5" Height="400" Canvas.Top="80" Canvas.Left="350" Template="{StaticResource DesignerItemTemplateLine}"&gt; &lt;Rectangle Fill="Blue" IsHitTestVisible="False" Name="mRect"&gt; &lt;Rectangle.RenderTransform&gt; &lt;TransformGroup&gt; &lt;RotateTransform Angle="45"/&gt; &lt;/TransformGroup&gt; &lt;/Rectangle.RenderTransform&gt; &lt;/Rectangle&gt; &lt;/ContentControl&gt; </code></pre> <p>C# code</p> <pre><code> private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e) { Control designerItem = this.DataContext as Control; if (designerItem != null) { double left = Canvas.GetLeft(designerItem); double top = Canvas.GetTop(designerItem); if (mRect != null)// This line don't compile Canvas.SetLeft(designerItem, left + e.HorizontalChange); Canvas.SetTop(designerItem, top + e.HorizontalChange); //Canvas.SetTop(designerItem, top + e.VerticalChange); } } </code></pre> <p>You can notice that "if (mRect != null)" <strong>does not</strong> pass compilation</p> <p>-------------Seconde Edit--- All the code----------------------------</p> <pre><code>&lt;Window x:Class="DiagramDesigner.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:DiagramDesigner" WindowStartupLocation="CenterScreen" Title="Move" Height="550" Width="750"&gt; </code></pre> <p></p> <pre><code>&lt;!-- MoveThumb Template --&gt; &lt;ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumbUpDwon}"&gt; &lt;Rectangle Fill="Transparent"/&gt; &lt;/ControlTemplate&gt; &lt;!-- MoveThumb Template --&gt; &lt;ControlTemplate x:Key="MoveThumbTemplateLeftRight" TargetType="{x:Type s:MoveThumbLeftRight}"&gt; &lt;Rectangle Fill="Transparent"/&gt; &lt;/ControlTemplate&gt; &lt;!-- MoveThumb Template --&gt; &lt;ControlTemplate x:Key="MoveLineTemplate" TargetType="{x:Type s:MoveLine}"&gt; &lt;Rectangle Fill="Black"&gt; &lt;Rectangle.RenderTransform&gt; &lt;TransformGroup&gt; &lt;RotateTransform Angle="45"/&gt; &lt;/TransformGroup&gt; &lt;/Rectangle.RenderTransform&gt; &lt;/Rectangle&gt; &lt;/ControlTemplate&gt; &lt;!-- Designer Item Template--&gt; &lt;ControlTemplate x:Key="DesignerItemTemplate" TargetType="ContentControl"&gt; &lt;Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"&gt; &lt;s:MoveThumbUpDwon Template="{StaticResource MoveThumbTemplate}" Cursor="SizeAll"/&gt; &lt;ContentPresenter Content="{TemplateBinding ContentControl.Content}"/&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;ControlTemplate x:Key="DesignerItemTemplateLeftRight" TargetType="ContentControl"&gt; &lt;Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"&gt; &lt;s:MoveThumbLeftRight Template="{StaticResource MoveThumbTemplateLeftRight}" Cursor="SizeAll"/&gt; &lt;ContentPresenter Content="{TemplateBinding ContentControl.Content}"/&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;ControlTemplate x:Key="DesignerItemTemplateLine" TargetType="ContentControl"&gt; &lt;Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"&gt; &lt;s:MoveLine Template="{StaticResource MoveLineTemplate}" Cursor="SizeAll"/&gt; &lt;ContentPresenter Content="{TemplateBinding ContentControl.Content}"/&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Window.Resources&gt; &lt;Canvas&gt; &lt;ContentControl Width="600" Height="5" Canvas.Top="250" Canvas.Left="80" Template="{StaticResource DesignerItemTemplate}"&gt; &lt;Rectangle Fill="Blue" IsHitTestVisible="False"/&gt; &lt;/ContentControl&gt; &lt;ContentControl Width="5" Height="400" Canvas.Top="80" Canvas.Left="350" Template="{StaticResource DesignerItemTemplateLeftRight}"&gt; &lt;Rectangle Fill="Blue" IsHitTestVisible="False"/&gt; &lt;/ContentControl&gt; &lt;ContentControl Width="5" Height="400" Canvas.Top="80" Canvas.Left="350" Template="{StaticResource DesignerItemTemplateLine}"&gt; &lt;Rectangle Fill="Blue" IsHitTestVisible="False" Name="mRect"&gt; &lt;Rectangle.RenderTransform&gt; &lt;TransformGroup&gt; &lt;RotateTransform Angle="45"/&gt; &lt;/TransformGroup&gt; &lt;/Rectangle.RenderTransform&gt; &lt;/Rectangle&gt; &lt;/ContentControl&gt; &lt;/Canvas&gt; </code></pre> <p></p> <p>Now the C# code</p> <pre><code>using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Shapes; using System.Windows.Media; using System.Windows; namespace DiagramDesigner { public class MoveThumbUpDwon : Thumb { public MoveThumbUpDwon() { DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta); } private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e) { Control designerItem = this.DataContext as Control; if (designerItem != null) { double left = Canvas.GetLeft(designerItem); double top = Canvas.GetTop(designerItem); //Canvas.SetLeft(designerItem, left + e.HorizontalChange); Canvas.SetTop(designerItem, top + e.VerticalChange); } } } public class MoveThumbLeftRight : Thumb { public MoveThumbLeftRight() { DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta); } private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e) { Control designerItem = this.DataContext as Control; if (designerItem != null) { double left = Canvas.GetLeft(designerItem); double top = Canvas.GetTop(designerItem); Canvas.SetLeft(designerItem, left + e.HorizontalChange); //Canvas.SetTop(designerItem, top + e.VerticalChange); } } } public class MoveLine : Thumb { public MoveLine() { DragDelta += new DragDeltaEventHandler(this.MoveThumb_DragDelta); } private void MoveThumb_DragDelta(object sender, DragDeltaEventArgs e) { Control designerItem = this.DataContext as Control; if (designerItem != null) { double left = Canvas.GetLeft(designerItem); double top = Canvas.GetTop(designerItem); // if (mRect != null) Canvas.SetLeft(designerItem, left + e.HorizontalChange); Canvas.SetTop(designerItem, top + e.HorizontalChange); //Canvas.SetTop(designerItem, top + e.VerticalChange); } } } </code></pre> <p>}</p>
 

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