?
Solved

how to make label in WPF 3.5 red or invisible

Posted on 2009-05-14
8
Medium Priority
?
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 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