Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get variables in the class used in Style in WPF?
    primarykey
    data
    text
    <p>In my MainWindow, there's ContentControl defined like this:</p> <pre><code>&lt;ContentControl x:Name="rectangle" Width="150" Height="150" Canvas.Top="150" Canvas.Left="470" Selector.IsSelected="True" Style="{StaticResource DesignerItemStyle}"&gt; &lt;Rectangle Fill="Blue" Stretch="Fill" IsHitTestVisible="False" /&gt; &lt;/ContentControl&gt; </code></pre> <p>and the DesignerItem.xaml is (which used to class called MoveThumb and RotateThumb:</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dr="clr-namespace:DragAndRotate"&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="MoveThumb.xaml" /&gt; &lt;ResourceDictionary Source="RotateDecorator.xaml" /&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;Style x:Key="DesignerItemStyle" TargetType="ContentControl"&gt; &lt;Setter Property="MinHeight" Value="50" /&gt; &lt;Setter Property="MinWidth" Value="50" /&gt; &lt;Setter Property="RenderTransformOrigin" Value="0.5,0.5" /&gt; &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ContentControl"&gt; &lt;Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"&gt; &lt;Control Name="RotateDecorator" Template="{StaticResource RotateDecoratorTemplate}" Visibility="Collapsed" /&gt; &lt;dr:MoveThumb Template="{StaticResource MoveThumbTemplate}" Cursor="SizeAll" /&gt; &lt;ContentPresenter Content="{TemplateBinding ContentControl.Content}" /&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="Selector.IsSelected" Value="True"&gt; &lt;Setter TargetName="RotateDecorator" Property="Visibility" Value="Visible" /&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p></p> <p>And I want to get the angle of rectangle which will be changed during rotating and binding it to a textbox in MainWindow. I tried a global variable by creating a new class called bridge implements INotifyPropertyChanged including the real time changed variable by the RotateThumb and bind it to TextBox in MainWindow but it doesn't work, which is also not elegant in my opinion. </p> <p>Is there any way to get the changing variable in class RotateThumb and bind it to TextBox? Thank you in advance.</p> <p>RotateDecorator.xaml just provides metaphor to show the Control can be drag to rotate. And below is RotateThumb and I wanna rotateTransform.Angle changed in DragDelta:</p> <pre><code>public class RotateThumb : Thumb { private Point centerPoint; private Vector startVector; private double initialAngle; private Canvas designerCanvas; private ContentControl designerItem; private RotateTransform rotateTransform; public RotateThumb() { DragDelta += new DragDeltaEventHandler(RotateThumb_DragDelta); DragStarted += new DragStartedEventHandler(RotateThumb_DragStarted); } private void RotateThumb_DragStarted(object sender, DragStartedEventArgs e) { this.designerItem = DataContext as ContentControl; if (this.designerItem != null) { this.designerCanvas = VisualTreeHelper.GetParent(designerItem) as Canvas; if (this.designerCanvas != null) { this.centerPoint = this.designerItem.TranslatePoint(new Point(this.designerItem.Width * this.designerItem.RenderTransformOrigin.X, this.designerItem.Height * this.designerItem.RenderTransformOrigin.Y), this.designerCanvas); Point startPoint = Mouse.GetPosition(this.designerCanvas); this.startVector = Point.Subtract(startPoint, this.centerPoint); this.rotateTransform = this.designerItem.RenderTransform as RotateTransform; if (this.rotateTransform == null) { this.designerItem.RenderTransform = new RotateTransform(0); this.initialAngle = 0; } else this.initialAngle = this.rotateTransform.Angle; } } } void RotateThumb_DragDelta(object sender, DragDeltaEventArgs e) { if (this.designerItem != null &amp;&amp; this.designerCanvas != null) { Point current = Mouse.GetPosition(this.designerCanvas); Vector deltaVector = Point.Subtract(current, this.centerPoint); double angle = Vector.AngleBetween(this.startVector, deltaVector); RotateTransform rotateTransform = this.designerItem.RenderTransform as RotateTransform; rotateTransform.Angle = this.initialAngle + Math.Round(angle, 0); this.designerItem.InvalidateMeasure(); } } } </code></pre> <p>And below is the Bridge, 'cause I don't how to angle in RotateThumb, so I create "bridge" and hope MainWindow.Textbox's context can be bind to it:</p> <pre><code>public class Bridge : INotifyPropertyChanged { private string angletext; public string Angle { get { return angletext; } set { if (value != angletext) { angletext = value; Console.WriteLine(angletext); OnPropertyChanged(new PropertyChangedEventArgs("Angle")); } } } public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) { PropertyChanged(this, e); } } public event PropertyChangedEventHandler PropertyChanged; public static Bridge bridge = new Bridge(); </code></pre>
    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.
    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