0

在我的 MainWindow 中,ContentControl 定义如下:

<ContentControl x:Name="rectangle"  Width="150" Height="150" Canvas.Top="150" Canvas.Left="470" Selector.IsSelected="True" Style="{StaticResource DesignerItemStyle}">
    <Rectangle Fill="Blue" Stretch="Fill" IsHitTestVisible="False" />
</ContentControl>

并且 DesignerItem.xaml 是(用于类称为 MoveThumb 和 RotateThumb:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:dr="clr-namespace:DragAndRotate">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="MoveThumb.xaml" />
    <ResourceDictionary Source="RotateDecorator.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style x:Key="DesignerItemStyle" TargetType="ContentControl">
    <Setter Property="MinHeight" Value="50" />
    <Setter Property="MinWidth" Value="50" />
    <Setter Property="RenderTransformOrigin" Value="0.5,0.5" />
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                    <Control Name="RotateDecorator"
                             Template="{StaticResource RotateDecoratorTemplate}"
                             Visibility="Collapsed" />
                    <dr:MoveThumb Template="{StaticResource MoveThumbTemplate}" Cursor="SizeAll" />
                    <ContentPresenter Content="{TemplateBinding ContentControl.Content}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="Selector.IsSelected" Value="True">
                        <Setter TargetName="RotateDecorator" Property="Visibility" Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我想获得在旋转过程中会改变的矩形的角度,并将其绑定到 MainWindow 中的文本框。我通过创建一个名为 bridge 的新类来尝试一个全局变量,实现 INotifyPropertyChanged 包括由 RotateThumb 实时更改的变量并将其绑定到 MainWindow 中的 TextBox 但它不起作用,这在我看来也不优雅。

有什么方法可以获取 RotateThumb 类中的更改变量并将其绑定到 TextBox?先感谢您。

RotateDecorator.xaml 只是提供了隐喻来显示控件可以拖动旋转。下面是RotateThumb,我想在DragDelta中改变rotateTransform.Angle:

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 && 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();
        }
    }
}

下面是桥,因为我不知道如何在 RotateThumb 中倾斜,所以我创建了“桥”并希望 MainWindow.Textbox 的上下文可以绑定到它:

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();
4

1 回答 1

0

您的情况是普通绑定有些无用的情况之一。

在 RotateThumb 类中,我会公开一个定制的 RoutedCommand,它会在每次拇指变化时触发一个携带当前角度的命令。由于 RoutedCommand 在可视化树中冒泡,因此很容易在 Window 中捕获传入的通知。

唯一的(相对的)问题是在视觉树完全构建后立即得到通知。由于没有(假定的)拇指值更改,因此不会通知拥有窗口。但是,您可以假设窗口已知初始(默认)拇指值。

希望能帮助到你。

于 2013-09-21T05:22:51.903 回答