【问题标题】:How to change visual state from DependencyPropertyChanged handler?如何从 Dependency Property Changed 处理程序更改视觉状态?
【发布时间】:2015-07-28 21:02:53
【问题描述】:

第一次从 DependencyPropertyChanged 处理程序调用时,视觉状态不会改变。

当通过按钮单击或其他事件触发时,相同的视觉状态会起作用...

依赖属性

        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { this.SetValue(IsSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(NumericTextBlock), new PropertyMetadata(null, OnIsSelectedChanged));

        /// <summary>
        /// change event handler, fires when IsSelected property changes
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NumericTextBlock textBlock = d as NumericTextBlock;
            if (d != null)
            {
                bool isSelected = (bool)(e.NewValue ?? false);
                if (isSelected)
                {                    
                    VisualStateManager.GoToState(textBlock, "Selected", true);
                }
                else
                {
                    if (string.IsNullOrWhiteSpace( textBlock.valueTextBlock.Text))
                    {
                        VisualStateManager.GoToState(textBlock, "Normal", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(textBlock, "Edit", true);
                    }
                }
            }
        }

为控件设置 IsSelected

<custom:NumericTextBlock IsSelected="True"></custom:NumericTextBlock>

【问题讨论】:

    标签: c# xaml windows-store-apps dependency-properties


    【解决方案1】:

    显然,只需要这样做:

            protected override void OnApplyTemplate()
            {
                ...
                base.OnApplyTemplate();
                if (IsSelected)
                {
                    VisualStateManager.GoToState(this, "Selected", true);
                }
             }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      相关资源
      最近更新 更多