Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在装饰器和装饰控件之间交换参数_C#_Wpf_Parameter Passing_Adorner - Fatal编程技术网

C# 在装饰器和装饰控件之间交换参数

C# 在装饰器和装饰控件之间交换参数,c#,wpf,parameter-passing,adorner,C#,Wpf,Parameter Passing,Adorner,我需要在装饰器和装饰控件之间传递一些参数 如何做到这一点?我是否应该在每次参数更改时删除并添加具有新参数的新装饰器 例如,我的一个参数: public static readonly DependencyProperty ThetaProperty = DependencyProperty.Register("Theta", typeof (double), typeof (SplitControl), new PropertyMetadata(default(doubl

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

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

例如,我的一个参数:

    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;
        }
    }
下面是一个示例(在文本框中键入一些内容并查看装饰器):

代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
利用制度全球化;
名称空间装饰器
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
这个。加载的+=(o,e)=>
{
AdornerLayer=AdornerLayer.GetAdornerLayer(this.t);
MyAdorner MyAdorner=新的MyAdorner(this.t);
添加(myAdorner);
this.t.Text=“修改值”;
};
}
}
公共类MyAdorner:Adorner
{
公共静态从属属性TextProperty=
DependencyProperty.Register(“文本”,
类型(字符串),
类型(MyAdorner),
新属性元数据(“默认文本”,
(o,e)=>
{
((MyAdorner)o).InvalidateVisual();
}));
//确保调用基类构造函数。
公共MyAdorner(UIElement AdorneElement)
:基础(装饰元素)
{
this.DataContext=this.AdornedElement;
这是SetUpBindings();
}
私有void SetUpBindings()
{
BindingOperations.SetBinding(此,
MyAdorner.TextProperty,
新绑定()
{
路径=新属性路径(“文本”),
UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged
});
}
公共字符串文本
{
获取{返回(字符串)this.GetValue(MyAdorner.TextProperty);}
设置{this.SetValue(MyAdorner.TextProperty,value);}
}
受保护的重写void OnRender(DrawingContext DrawingContext)
{
Rect adornedlementrect=new Rect(this.adornedlement.DesiredSize);
drawingContext.DrawText(新的格式化文本(this.Text、CultureInfo.CurrentCulture、,
FlowDirection.LeftToRight,
新字体(“Arial”),
20, 
(黑色),
新的点(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>

以下是一个示例(在文本框中键入一些内容,然后观察装饰器):

代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
利用制度全球化;
名称空间装饰器
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
这个。加载的+=(o,e)=>
{
AdornerLayer=AdornerLayer.GetAdornerLayer(this.t);
MyAdorner MyAdorner=新的MyAdorner(this.t);
添加(myAdorner);
this.t.Text=“修改值”;
};
}
}
公共类MyAdorner:Adorner
{
公共静态从属属性TextProperty=
DependencyProperty.Register(“文本”,
类型(字符串),
类型(MyAdorner),
新属性元数据(“默认文本”,
(o,e)=>
{
((MyAdorner)o).InvalidateVisual();
}));
//确保调用基类构造函数。
公共MyAdorner(UIElement AdorneElement)
:基础(装饰元素)
{
this.DataContext=this.AdornedElement;
这是SetUpBindings();
}
私有void SetUpBindings()
{
BindingOperations.SetBinding(此,
MyAdorner.TextProperty,
新绑定()
{
路径=新属性路径(“文本”),
UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged
});
}
公共字符串文本
{
获取{返回(字符串)this.GetValue(MyAdorner.TextProperty);}
设置{this.SetValue(MyAdorner.TextProperty,value);}
}
受保护的重写void OnRender(DrawingContext DrawingContext)
{
Rect adornedlementrect=new Rect(this.adornedlement.DesiredSize);
drawingContext.DrawText(新的格式化文本(this.Text、CultureInfo.CurrentCulture、,
FlowDirection.LeftToRight,
新字体(“Arial”),
20, 
(黑色),
新的点(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是一个DP,因此它可以享受数据绑定的全部好处,这在我的示例参数中得到了演示