Solved

how to make label in WPF 3.5 red or invisible

Posted on 2009-05-14
8
708 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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