zoukankan      html  css  js  c++  java
  • change textblock background color when text equal to referenceValue

    cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    
    namespace MCE.Gems.Common.Controls.CustomControls
    {
        /// <summary>
        /// change textblock background color when text equal to referenceValue
        /// </summary>
        public class MyTextBlock : TextBlock
        {
            #region DependencyProperty
    
    
            public string ReferenceValue
            {
                get { return (string)GetValue(ReferenceValueProperty); }
                set { SetValue(ReferenceValueProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for ReferenceValue.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ReferenceValueProperty =
                DependencyProperty.Register("ReferenceValue", typeof(string), typeof(MyTextBlock), new PropertyMetadata(null, new PropertyChangedCallback(OnReferenceValueChanged)));
    
            private static void OnReferenceValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                MyTextBlock mb = d as MyTextBlock;
                if (mb.DisplayText!=null)
                {
                    if (mb.DisplayText.ToString().Equals(e.NewValue.ToString()))
                    {
                        mb.Background = mb.EqualBackground;
                    }
                }
              
            }
    
    
    
            public string DisplayText
            {
                get { return (string)GetValue(DisplayTextProperty); }
                set { SetValue(DisplayTextProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for DisplayText.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty DisplayTextProperty =
                DependencyProperty.Register("DisplayText", typeof(string), typeof(MyTextBlock), new PropertyMetadata(null, new PropertyChangedCallback(OnDisplayTextChanged)));
    
            private static void OnDisplayTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                MyTextBlock mb = d as MyTextBlock;
                mb.Text = e.NewValue.ToString();
                
                if (mb.ReferenceValue!=null)
                {
                    if (mb.ReferenceValue.ToString().Equals(e.NewValue.ToString()))
                    {
                        mb.Background = mb.EqualBackground;
                    }
                }
               
            }
    
            
    
    
    
            public Brush EqualBackground
            {
                get { return (Brush)GetValue(EqualBackgroundProperty); }
                set { SetValue(EqualBackgroundProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for EqualBackground.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty EqualBackgroundProperty =
                DependencyProperty.Register("EqualBackground", typeof(Brush), typeof(MyTextBlock), new PropertyMetadata(null, new PropertyChangedCallback(OnEqualBackgroundChanged)));
    
            private static void OnEqualBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                MyTextBlock mb = d as MyTextBlock;
          
                if (mb.ReferenceValue != null&&mb.DisplayText!=null)
                {
                    if (mb.ReferenceValue.ToString().Equals(mb.DisplayText.ToString()))
                    {
                        mb.Background = e.NewValue as Brush;
                    }
                }
    
            }
    
            #endregion
            
           
        }
    }
    View Code
  • 相关阅读:
    SVN中Revert changes from this revision 跟Revert to this revision
    bootstrap中如何使input中的小图标获得点击事件
    基于Bootstrap使用jQuery实现输入框组input-group的添加与删除-改进版
    MVC4 Controller 与 WebApi 的 Session 传值问
    MVC Razor与javascript混编(js中嵌入razor)
    Asp.net mvc怎么在razor里写js代码
    MVC后台数据赋值给前端JS对象
    ASP.NET MVC自定义验证Authorize Attribute(包含cookie helper)
    ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存
    【记录】【solr】solr7.2.1原子更新
  • 原文地址:https://www.cnblogs.com/FaDeKongJian/p/3099120.html
Copyright © 2011-2022 走看看