Solved

how to make label in WPF 3.5 red or invisible

Posted on 2009-05-14
8
691 Views
Last Modified: 2013-11-08
I am a long time web developer using Visual Studio.  Now a client wants me to develop a WPF 3.5 application to go with the website.  I am encountering an issue that I have never experienced before:

When a user doesn't input information I am used to either lblError.text.forecolor = drawing.color.red OR something like lblError.visible = true.

Neither of these work and it flags me with an error message "it is in read only mode" so it cannot be altered.

Understanding this is very trivial but I need good input on a standard, please don't laugh when you answer this!  :)

Thanks in advance for the help,

B
0
Comment
Question by:cyimxtck
  • 5
  • 2
8 Comments
 

Expert Comment

by:Infog
ID: 24384919
If the "Read-only mode" message comes up when you are running the program, it is be because you can't both run the program and change the code at the same time. So, either use a breakpoint to pause code at a certain line to change it, or change the code while the program is not running.

The code I included should answer your other question.
Me.lblError.ForeColor = Color.Red
Me.lblError.visible = True

Open in new window

0
 

Author Comment

by:cyimxtck
ID: 24385089
Those options are not available when you are using WPF.  For a web application yes; I am used to using what you have above but in Windows Presentation Foundation they are not options.

Thanks,

B
0
 

Author Comment

by:cyimxtck
ID: 24385189
How do I turn the label red is what I really need to know.

Is this possible?

I just figured out the syntax for hiding it is:

lblLastName.Visibility = Windows.Visibility.Hidden
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Expert Comment

by:ToddBeaulieu
ID: 24385486
In Wpf, you really want to embrace the new way of doing things, and having code manipulate controls instead of using binding is not usually the best way to accomplish this.

You start by evaluating what the conditions are that will cause the control to change its appearance. Then make a property on the DataContext for that condition, being sure it implements IChangeNotification. In the code behind, set the property, which will raise the notification event. The ui will see that change and evalute the binding.

So, you can then put a trigger on the control that, when the condition is met, sets the color. All that code would be in the XAML. Appearance-related logic is best in the UI layer. I know the code-behind can be considered the UI layer (IMO), but still ... template triggers are good to use.

The example below is a data template with a trigger than changes an icon based on the object's Status property. It's not the simplest example, but should get you started thinking about this concept.
    <Style x:Key="mapStatusIconStyle" TargetType="{x:Type igDP:CellValuePresenter}">
        <Style.Triggers>
 
            <!-- UNMAPPED ICON -->
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Record.DataItem.MapStatus}" Value="Unmapped">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
                            <ContentControl Template="{StaticResource unmappedIcon}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
 
            <!-- MAPPED ICON -->
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Record.DataItem.MapStatus}" Value="Mapped">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
                            <ContentControl Template="{StaticResource mappedIcon}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
 
            <!-- SUGGESTED ICON -->
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Record.DataItem.MapStatus}" Value="Suggested">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
                            <ContentControl Template="{StaticResource suggestedIcon}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
 
        </Style.Triggers>
    </Style>

Open in new window

0
 

Author Comment

by:cyimxtck
ID: 24385538
I have two questions on the above example:

1) where can I store this such that I create it once instead of every single page since I am really just calling out "red" as a color I want the UI to have in a label

2) how do I physically set the value with the above example?  Where do I call out "red" and how do I do that in the VB code behind?  Can I access the "Template" in the code behind?

Thanks,

B
0
 
LVL 16

Accepted Solution

by:
ToddBeaulieu earned 500 total points
ID: 24386012
Yeah, this isn't easy stuff to GET overnight, unfortunately.

You can create a shared ResourceDictionary and then merge that into a window/page/etc. (see first chunk of xaml)

ALso shown below is an example of how I made a data loading indicator on the screen and bound it's Visiblity property to a property in the DataContext (# 2). This uses straight binding, instead of data triggers, and therefore needs a converter to convert my boolean property (true/false) to a value suitable for the Visibility property. SHown in # 3 is the converter.

There are a lot of concepts here ... sorry...

So check this linke out: http://www.switchonthecode.com/tutorials/xaml-tutorial-changing-text-color-on-mouse-over

It shows a Brush resource and using it with a trigger.
    <TabItem.Resources>
        <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MySharedResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </TabItem.Resources>
 
 
------ # 2
 
<TextBlock ...
    Visibility="{Binding IsBusyLoadingData, Converter={StaticResource visiblityConverter}}"
    >
    LOADING DATA ...
</TextBlock>
 
 
------- # 3
 
    public class VisiblityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool b = (bool)value;
        return b ? Visibility.Visible : Visibility.Collapsed;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
 
    }

Open in new window

0
 

Author Comment

by:cyimxtck
ID: 24386130
Great I have some reading to do but this makes sense to me!

Thanks,

B
0
 

Author Closing Comment

by:cyimxtck
ID: 31581439
Great resource for getting things done like I need to do!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now