如何绑定到资源sys:Double?

时间:2015-10-13 15:25:48

标签: c# xaml data-binding dependency-properties

我是stackoverflow中的新手。我希望在这里,我会找到答案。我的问题:我创建了一个用户控件。在资源方面有

<sys:Double x:Key="BRadiusX" >5</sys:Double>

在控制模板中:

<ControlTemplate TargetType="{x:Type Thumb}">
                    <Rectangle x:Name="Ellipse"
                        StrokeThickness="1"
                               RadiusX="{DynamicResource BRadiusX}"
                               RadiusY="{DynamicResource BRadiusY}">.....

现在我要将5绑定到 代码中的DependancyProperty:

public static DependencyProperty BorderRadiusXProperty = DependencyProperty.Register("BorderRadiusX", typeof(double), typeof(MySlider1),
    new FrameworkPropertyMetadata(5.0, FrameworkPropertyMetadataOptions.AffectsRender));
    [Category("Thumb"), Description("XRadius of border round the thumb")]
    public double BorderRadiusX
    {
        get { return (double)GetValue(BorderRadiusXProperty); }


        set { SetValue(BorderRadiusXProperty, value); }
    }

有什么方法可以解决这个问题吗?

非常感谢转发

2 个答案:

答案 0 :(得分:0)

在代码(例如构造函数)中,您可以:

double radius = (double)Resources["BRadiusX"]

然后你可以:

BorderRadiusX = radius;

它不是一个真正的数据绑定,它只是ressource查找。 如果你需要数据绑定,它会更复杂,但对于不可变的双重,我不确定它是否对数据绑定很有用。

Resources是Application,FrameworkElements的属性,如Windows,UserControls,Grid。

此致

答案 1 :(得分:0)

 public MySlider1()
    {
        InitializeComponent();

        Resources["BRadiusX"] = BorderRadiusX;
        Resources["BRadiusY"] = BorderRadiusY;
    }

   protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);

        Resources["BRadiusX"] = BorderRadiusX;
        Resources["BRadiusY"] = BorderRadiusY;
    }

    public static DependencyProperty BorderRadiusXProperty = DependencyProperty.Register("BorderRadiusX", typeof(double), typeof(MySlider1),
    new FrameworkPropertyMetadata(5.0, FrameworkPropertyMetadataOptions.AffectsRender));
    [Category("Thumb"), Description("XRadius of border round the thumb")]
    public double BorderRadiusX
    {
        get { return (double)GetValue(BorderRadiusXProperty); }


        set { SetValue(BorderRadiusXProperty, value); }
    }

    public static DependencyProperty BorderRadiusYProperty = DependencyProperty.Register("BorderRadiusY", typeof(double), typeof(MySlider1),
    new FrameworkPropertyMetadata(5.0, FrameworkPropertyMetadataOptions.AffectsRender));
    [Category("Thumb"), Description("YRadius of border round the thumb")]
    public double BorderRadiusY
    {
        get { return (double)GetValue(BorderRadiusYProperty); }


        set { SetValue(BorderRadiusYProperty, value); }
    }