在 Silverlight 中转换 WPF 控件

发布于 2024-12-09 03:41:02 字数 4576 浏览 0 评论 0原文

Hyper Tree 有一个非常古老的 WPF 应用程序 - http://blogs.msdn.com/b/llobo/archive/2007/10/31/mindmap-app-using-hyperbolic-tree.aspx

源代码可以在 codeplax.com 找到 - http://hypertree.codeplex.com/releases/view/11524

我想使用我的 silverlight 应用程序中的这个树控件。现在的问题是我是 silverlight 的新手,并且代码使用了一些 WPF 特定的东西。

请建议我解决我的问题。

提前致谢。

Abhinav

更新:

诸如此类的事情 FrameworkPropertyMetadataFrameworkPropertyMetadataOptionsInvalidateVisual()OnRender 覆盖、子 UIElements

添加代码:

public class SmartBorder : Decorator
{
    #region Dependency Properties
    public static readonly DependencyProperty GlowBrushProperty =
        DependencyProperty.Register("GlowBrush", typeof(Brush), typeof(SmartBorder), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));

  ......

    #region Dependency Property backing CLR properties
......
    #endregion

    // if the button is pressed, this fires
    private static void OnRenderIsPressedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        SmartBorder border = o as SmartBorder;
        if (border != null)
        {
            if ((bool)e.NewValue == true)
            {
                border.BorderBrush = Brushes.Transparent;
                border.BorderWidth = 2;
            }
            else
            {
                border.BorderBrush = Brushes.Red;
                border.BorderWidth = 2;

            }
            border.InvalidateVisual();
        }
    }

    // if the mouse is over the control, this fires
    private static void OnRenderIsMouseOverChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        SmartBorder border = o as SmartBorder;
        if (border != null)
        {
            border.InvalidateVisual();
        }

    }

    // a series of methods which all make getting the default or currently selected brush easier


    protected override void OnRender(DrawingContext dc)
    {
        Rect rc = new Rect(0, 0, this.ActualWidth, this.ActualHeight);

        LinearGradientBrush gradientOverlay = GetGradientOverlay();
        Brush glowBrush = GetGlowBrush();
        Brush backBrush = GetBackgroundBrush();
        Brush borderBrush = GetBorderBrush();
        Pen borderPen = new Pen(borderBrush, BorderWidth);
        double cornerRadiusCache = CornerRadius;

        // draw the highlight as necessary
        if (RenderIsMouseOver)
        {
            Rect rcGlow = rc;
            double glowMove = BorderWidth * 2;
            rcGlow.Inflate(glowMove, glowMove);
            glowMove = 0;
            rcGlow.Offset(new Vector(glowMove, glowMove));
            dc.DrawRoundedRectangle(GetOuterGlowBrush(), null, rcGlow, cornerRadiusCache, cornerRadiusCache);
        }

        // we want to clip anything that might errantly draw outside of the smart border control
        dc.PushClip(new RectangleGeometry(rc, cornerRadiusCache, cornerRadiusCache));

        dc.DrawRoundedRectangle(backBrush, borderPen, rc, cornerRadiusCache, cornerRadiusCache);
        dc.DrawRoundedRectangle(gradientOverlay, borderPen, rc, cornerRadiusCache, cornerRadiusCache);

        if (!RenderIsPressed)
        {
            double clipBorderSize = BorderWidth * -4.0;
            Rect rcClip = rc;
            rcClip.Offset(clipBorderSize, clipBorderSize);
            rcClip.Inflate(-clipBorderSize, -clipBorderSize);
            dc.PushClip(new RectangleGeometry(rcClip, cornerRadiusCache, cornerRadiusCache));
            dc.DrawEllipse(glowBrush, null, new Point(this.ActualWidth / 2, this.ActualHeight * 0.10), this.ActualWidth * 0.80, this.ActualHeight * 0.40);
            dc.Pop();
        }
        // just draw the border now to make sure it overlaps everything nicely
        dc.DrawRoundedRectangle(null, borderPen, rc, cornerRadiusCache, cornerRadiusCache);

        dc.Pop();
        //base.OnRender(drawingContext);
    }


    protected override Size MeasureOverride(Size constraint)
    {
        UIElement child = this.Child as UIElement;

        double borderThickness = BorderWidth * 2.0;

        if (child != null)
        {

    ...
        }

        return new Size(Math.Min(borderThickness, constraint.Width), Math.Min(borderThickness, constraint.Height));
    }
}

There is an very old WPF application of Hyper Tree - http://blogs.msdn.com/b/llobo/archive/2007/10/31/mindmap-app-using-hyperbolic-tree.aspx.

The source code can be found at codeplax.com -
http://hypertree.codeplex.com/releases/view/11524

I wanted to use this tree control in my silverlight application. Now the issue is that i am new to silverlight, and the code is using some WPF specific things.

Please suggest me to solve my problem.

Thanks in advance.

Abhinav

Update:

things like
FrameworkPropertyMetadata and FrameworkPropertyMetadataOptions, InvalidateVisual(), OnRender override, child UIElements.

Code Added:

public class SmartBorder : Decorator
{
    #region Dependency Properties
    public static readonly DependencyProperty GlowBrushProperty =
        DependencyProperty.Register("GlowBrush", typeof(Brush), typeof(SmartBorder), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));

  ......

    #region Dependency Property backing CLR properties
......
    #endregion

    // if the button is pressed, this fires
    private static void OnRenderIsPressedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        SmartBorder border = o as SmartBorder;
        if (border != null)
        {
            if ((bool)e.NewValue == true)
            {
                border.BorderBrush = Brushes.Transparent;
                border.BorderWidth = 2;
            }
            else
            {
                border.BorderBrush = Brushes.Red;
                border.BorderWidth = 2;

            }
            border.InvalidateVisual();
        }
    }

    // if the mouse is over the control, this fires
    private static void OnRenderIsMouseOverChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        SmartBorder border = o as SmartBorder;
        if (border != null)
        {
            border.InvalidateVisual();
        }

    }

    // a series of methods which all make getting the default or currently selected brush easier


    protected override void OnRender(DrawingContext dc)
    {
        Rect rc = new Rect(0, 0, this.ActualWidth, this.ActualHeight);

        LinearGradientBrush gradientOverlay = GetGradientOverlay();
        Brush glowBrush = GetGlowBrush();
        Brush backBrush = GetBackgroundBrush();
        Brush borderBrush = GetBorderBrush();
        Pen borderPen = new Pen(borderBrush, BorderWidth);
        double cornerRadiusCache = CornerRadius;

        // draw the highlight as necessary
        if (RenderIsMouseOver)
        {
            Rect rcGlow = rc;
            double glowMove = BorderWidth * 2;
            rcGlow.Inflate(glowMove, glowMove);
            glowMove = 0;
            rcGlow.Offset(new Vector(glowMove, glowMove));
            dc.DrawRoundedRectangle(GetOuterGlowBrush(), null, rcGlow, cornerRadiusCache, cornerRadiusCache);
        }

        // we want to clip anything that might errantly draw outside of the smart border control
        dc.PushClip(new RectangleGeometry(rc, cornerRadiusCache, cornerRadiusCache));

        dc.DrawRoundedRectangle(backBrush, borderPen, rc, cornerRadiusCache, cornerRadiusCache);
        dc.DrawRoundedRectangle(gradientOverlay, borderPen, rc, cornerRadiusCache, cornerRadiusCache);

        if (!RenderIsPressed)
        {
            double clipBorderSize = BorderWidth * -4.0;
            Rect rcClip = rc;
            rcClip.Offset(clipBorderSize, clipBorderSize);
            rcClip.Inflate(-clipBorderSize, -clipBorderSize);
            dc.PushClip(new RectangleGeometry(rcClip, cornerRadiusCache, cornerRadiusCache));
            dc.DrawEllipse(glowBrush, null, new Point(this.ActualWidth / 2, this.ActualHeight * 0.10), this.ActualWidth * 0.80, this.ActualHeight * 0.40);
            dc.Pop();
        }
        // just draw the border now to make sure it overlaps everything nicely
        dc.DrawRoundedRectangle(null, borderPen, rc, cornerRadiusCache, cornerRadiusCache);

        dc.Pop();
        //base.OnRender(drawingContext);
    }


    protected override Size MeasureOverride(Size constraint)
    {
        UIElement child = this.Child as UIElement;

        double borderThickness = BorderWidth * 2.0;

        if (child != null)
        {

    ...
        }

        return new Size(Math.Min(borderThickness, constraint.Width), Math.Min(borderThickness, constraint.Height));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-12-16 03:41:02

关于 Silverlight 的 FrameworkPropertyMetadataFrameworkPropertyMetadataOptions 以及值强制转换等,请参阅 ClipFlair 代码库下的 WPF_Compatibility 解决方案 (http://clipflair.codeplex.com)

Regarding FrameworkPropertyMetadata and FrameworkPropertyMetadataOptions and value coercions etc. for Silverlight see the WPF_Compatibility solution under the ClipFlair codebase (http://clipflair.codeplex.com)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文