【问题标题】:Exchange parameters between adorner and adorned control在装饰器和装饰控件之间交换参数
【发布时间】:2012-02-02 07:04:28
【问题描述】:

我需要在装饰器和装饰控件之间传递一些参数。

如何做到这一点?每次参数更改时,我是否应该删除并添加带有新参数的新装饰器?

比如我的一个参数:

    public static readonly DependencyProperty ThetaProperty =
        DependencyProperty.Register("Theta", typeof (double), typeof (SplitControl), new PropertyMetadata(default(double), SetTheta));

    public double Theta
    {
        get { return (double) GetValue(ThetaProperty); }
        set { SetValue(ThetaProperty, value); }
    }

    private static void SetTheta(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        _adorner.Theta = (double) e.NewValue;
    }

在装饰器 Theta 中:

    public double Theta
    {
        get
        {
            return (Math.Atan(((_middleTop - _middleBottom) / AdornedElement.DesiredSize.Height))) * 180 / Math.PI;
        }
        set
        {
            double deltaX = (Math.Tan((Math.PI/180)*value))*(AdornedElement.DesiredSize.Height/2);
            _middleTop = _middle + deltaX;
            _middleBottom = _middle - deltaX;
        }
    }

【问题讨论】:

  • 你想传递什么样的参数,你能举例说明一下吗?由于您有对装饰 UIElement 的引用,因此您应该能够从中访问最相关的信息。

标签: c# wpf parameter-passing adorner


【解决方案1】:

这是一个示例(在文本框中输入一些内容并观看装饰器):

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;

namespace Adorners
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += (o, e) => 
            {
                AdornerLayer layer = AdornerLayer.GetAdornerLayer(this.t);

                MyAdorner myAdorner = new MyAdorner(this.t);

                layer.Add(myAdorner);

                this.t.Text = "Modified Value";
            };
        }
    }


    public class MyAdorner : Adorner
    {
        public static DependencyProperty TextProperty =
            DependencyProperty.Register("Text",
            typeof(string),
            typeof(MyAdorner),
            new PropertyMetadata("Default Text", 
            (o, e) => 
            {
                ((MyAdorner)o).InvalidateVisual();
            }));

        // Be sure to call the base class constructor.
        public MyAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            this.DataContext = this.AdornedElement;

            this.SetUpBindings();
        }

        private void SetUpBindings()
        {
            BindingOperations.SetBinding(this,
               MyAdorner.TextProperty,
               new Binding()
               {
                   Path = new PropertyPath("Text"),
                   UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
               });
        }

        public string Text
        {
            get { return (string)this.GetValue(MyAdorner.TextProperty); }
            set { this.SetValue(MyAdorner.TextProperty, value); }
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize);

            drawingContext.DrawText(new FormattedText(this.Text, CultureInfo.CurrentCulture, 
                FlowDirection.LeftToRight, 
                new Typeface("Arial"), 
                20, 
                Brushes.Black), 
                new Point(0, 150));
        }
    }
}

标记:

<Window x:Class="Adorners.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="AdornedGrid">
        <TextBox x:Name="t" Width="200" Height="100" Background="Green"></TextBox>
    </Grid>
</Window>

【讨论】:

  • 我的参数是依赖属性,所以我不能使用 ref 将它们传递给装饰器。
  • 对不起,我不是指 ref 参数,我指的是在构造函数中注入的引用类型(如常规 CLR 对象,甚至是依赖项)。您是否将来自装饰元素的消息传递给 Adorner?
  • 有意义吗? Adorner 是一个 DP,因此它可以享受 DataBinding 的全部好处,这在我的示例中进行了演示
猜你喜欢
  • 2017-07-19
  • 1970-01-01
  • 2018-07-16
  • 2011-06-25
  • 2014-07-21
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多