Solved

how to make label in WPF 3.5 red or invisible

Posted on 2009-05-14
8
672 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

18 Experts available now in Live!

Get 1:1 Help Now