2012-01-04 65 views
0

我需要在裝飾者和裝飾控件之間傳遞一些參數。裝飾者和裝飾控件之間的交換參數

這是如何做到的?每次參數更改時,我是否應該使用新參數刪除並添加新的裝飾器?

例如,我的一個參數:

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; 
    } 

在裝飾器西塔:

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; 
     } 
    } 
+0

你想傳遞什麼類型的參數,你可以舉個例子來說明一下嗎?正如你參考了裝飾的UIElement,你應該能夠從中獲得最相關的信息。 – SvenG 2012-01-04 11:31:35

回答

3

下面是一個示例(類型的東西到文本框中,看裝飾器):

代碼:

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

Markup:

<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> 
+0

我的參數是依賴屬性,所以我不能使用ref將它們傳遞給裝飾者。 – ieaglle 2012-01-04 11:30:45

+0

對不起,我不是指ref參數,我的意思是在constrcutor中注入的引用類型(如在常規的CLR對象中,或者甚至是依賴的)。你是否將來自裝飾元素的消息傳遞給Adorner? – 2012-01-04 11:43:06

+0

謝謝,我會嘗試... – ieaglle 2012-01-04 12:11:52