当前位置: 代码迷 >> .NET Framework >> WPF自定义控件怎么在引用它的地方更改控件的宽度
  详细解决方案

WPF自定义控件怎么在引用它的地方更改控件的宽度

热度:103   发布时间:2016-05-01 23:34:42.0
WPF自定义控件如何在引用它的地方更改控件的宽度?
如题,在网上找的跑马灯自定义控件

ProgressControl.cs

......
using System.Windows.Threading;

namespace Marquee
{
    [TemplatePart(Name = "PART_ContentHost", Type = typeof(ContentPresenter))]
    public class ProgressControl: ContentControl
    {
        public int Loop
        {
            get { return (int)GetValue(LoopProperty); }
            set { SetValue(LoopProperty, value); }
        }
        public static readonly DependencyProperty LoopProperty = DependencyProperty.Register(
            "Loop",
            typeof(int),
            typeof(ProgressControl),
            new FrameworkPropertyMetadata(0, OnMarqueeParameterChanged, OnCoerceLoop));
        private static object OnCoerceLoop(DependencyObject sender, object value)
        {
            int loop = (int)value;
            if (loop < 0)
            {
                throw new ArgumentException("Loop必须大于等于0");
            }
            return value;
        }
        /// <summary>
        /// Gets/Sets the direction of the animation.
        /// </summary>
        public Direction Direction
        {
            get { return (Direction)GetValue(DirectionProperty); }
            set { SetValue(DirectionProperty, value); }
        }
        public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register(
            "Direction",
            typeof(Direction),
            typeof(ProgressControl),
            new FrameworkPropertyMetadata(Direction.Right, OnMarqueeParameterChanged));
        /// <summary>
        /// Gets/Sets The animation behavior.
        /// </summary>
        public MarqueeBehavior Behavior
        {
            get { return (MarqueeBehavior)GetValue(BehaviorProperty); }
            set { SetValue(BehaviorProperty, value); }
        }
        public static readonly DependencyProperty BehaviorProperty = DependencyProperty.Register(
            "Behavior",
            typeof(MarqueeBehavior),
            typeof(ProgressControl),
            new FrameworkPropertyMetadata(MarqueeBehavior.Scroll, OnMarqueeParameterChanged));
        public double ScrollAmount
        {
            get { return (double)GetValue(ScrollAmountProperty); }
            set { SetValue(ScrollAmountProperty, value); }
        }

        public static readonly DependencyProperty ScrollAmountProperty = DependencyProperty.Register(
            "ScrollAmount",
            typeof(double),
  相关解决方案