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
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:
oops, it didn’t like the presence of XAML markup…
<Image Source=”image.png” gi:ImageGreyer.IsGreyable=”true” />
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.
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.
ohhh… I see. Thanks.
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” });
wow i’m sorry! i’ve posted message above in wrong thread :-). it was concering FocusAttache sample on this site..