GreyableTextBlock

November 27, 2008

WPF naturaly doesn’t have image class, which supports Disable state. Alex P solved this problem. Label class support disable state, but TextBlock don’t. Sometimes you need to use TextBlock, not Label. So, here a little class GreyableTextBlock which addresed to this issue:

    public class GreyableTextBlock: TextBlock
    {
        static GreyableTextBlock()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(GreyableTextBlock), new FrameworkPropertyMetadata(typeof(GreyableTextBlock)));
            GreyableTextBlock.IsEnabledProperty.OverrideMetadata(typeof(GreyableTextBlock), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsEnabledChanged)));
        }

        private Brush ForegroundBrush;
        private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            GreyableTextBlock text = (GreyableTextBlock)d;
            if (e.Property.Name.Equals("IsEnabled"))
            {
                if ((e.NewValue as bool?) == false)
                {
                    text.ForegroundBrush = text.Foreground;
                    text.Foreground = Brushes.Gray;
                }
                else if ((e.NewValue as bool?) == true)
                {
                    text.Foreground = text.ForegroundBrush;
                }
            }
        }
    }

Advertisement

7 Responses to “GreyableTextBlock”

  1. Alex_P said

    I’ve implemented the same functionality (Image greyability that is) using attachable dependency properties. So that greyability can be attached to standard WPF Image control like this:

  2. Alex_P said

    oops, it didn’t like the presence of XAML markup…

    <Image Source=”image.png” gi:ImageGreyer.IsGreyable=”true” />

  3. iyalovoi said

    Thanks for replying. Actually, i read your article and use your control in my app, after that i came to idea to create GreyableTextBlock, because i need it as well. There is a link to your article (http://www.codeplex.com/GreyableImage) at codeplex at the begining of the article.

  4. Alex_P said

    I’m glad that you used the control!

    There is a bit of confusion I guess about my comment above :)

    What I mean is that in addition to the first implementation as a GreyableImage class, I have now implemented the same functionality differently – in purely WPF way, using attachable properties trick.

    It is there on the CodePlex if you’re interested.

  5. iyalovoi said

    ohhh… I see. Thanks.

  6. Boris said

    It’s possible to avoid additional class at all. i used it like this

    FrameworkElementFactory dtElement = new FrameworkElementFactory(typeof(TextBox)) { Name = “Txt” };
    dtElement.SetBinding(TextBox.TextProperty, binding);
    dtElement.SetValue(FocusManager.FocusedElementProperty, new Binding() { ElementName = “Txt” });

  7. Boris said

    wow i’m sorry! i’ve posted message above in wrong thread :-). it was concering FocusAttache sample on this site..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.