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
  • 相关阅读:
    【重大更新】Qlik Sense September 2018重磅发布(附下载)
    sharepoint多个NLB的web前段如何进行文件同步?
    通过组策略实现Firefox自动以当前域账号登录MOSS站点---(原创)
    EWS API 2.0读取日历信息-读取内容注意事项
    Dotfuscator混淆加密
    如何:在 SharePoint 中创建外部列表
    java学习笔记—HTTP协议(10)
    java学习笔记—Tomcat(9)
    java学习笔记DOM4J解析(7)
    php中mvc框架总结1(7)
  • 原文地址:https://www.cnblogs.com/FaDeKongJian/p/3099120.html
Copyright © 2011-2022 走看看