zoukankan      html  css  js  c++  java
  • wpf isEnabled属性从其container继承下来的问题

    Let's suppose we have the following xaml:
    <Grid IsEnabled = false>
        <TextBox IsEnabled =  true/>
    </Grid>

    Although the TextBox's IsEnabled property is set to true it will inherit it's value from it's container, in this case th Grid.

    Yes, but is it possible to redefine the IsEnabled property in the child controls?

    Solution:

    Strictly speaking, the IsEnabled property isn't inheritable -- it's defined on UIElement, whereas inheritance is a FrameworkElement concept.

    The way in which Avalon enforces this is via a CoerceValueCallback.  If the parent has IsEnabled is false, the child coerces it to be false as well.

    One reason we have the parent value cascade down to the children is that it's a common  for someone to want to disable an entire subtree and it would be a burden to force users to recursively go down and disable everything.

    If you want to disable an entire panel except a few controls I highly recommend you don't set IsEnabled=false on the parent and instead just disable the controls you want disabled.

    There is, however, a way to somewhat do what you want if you're willing to use subclass the controls you're using.  I would do this with caution, however.  Input may still work properly, but its by no means guaranteed.

    publicclassMyTextBox : TextBox

     

    {

      static MyTextBox()

      {

        MyTextBox.IsEnabledProperty.OverrideMetadata(typeof(MyTextBox),

        newUIPropertyMetadata(true,

        newPropertyChangedCallback(IsEnabledPropertyChanged),

        newCoerceValueCallback(CoerceIsEnabled)));

      }

      privatestaticvoid IsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)

      {

      }

      privatestaticobject CoerceIsEnabled(DependencyObject source, object value)

      {

        return value;

      }

    }


     

  • 相关阅读:
    python学习笔记Day3
    python学习笔记Day2
    IIS7.5部署除静态页面外都是404的解决方案
    CommandBehavior.CloseConnection有何作用
    没事别老待在家里
    冻结表格行列的思路
    如何构建逻辑清晰的可拖拽树的数据结构
    “仅次于20年前的最好的时间是现在“
    java的静态代理和2种动态代理(未完,待续)
    i++和++i
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1310355.html
Copyright © 2011-2022 走看看