how to make label in WPF 3.5 red or invisible

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
LVL 1
cyimxtckAsked:
Who is Participating?
 
ToddBeaulieuConnect With a Mentor Commented:
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
 
InfogCommented:
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
 
cyimxtckAuthor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
cyimxtckAuthor Commented:
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
 
ToddBeaulieuCommented:
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
 
cyimxtckAuthor Commented:
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
 
cyimxtckAuthor Commented:
Great I have some reading to do but this makes sense to me!

Thanks,

B
0
 
cyimxtckAuthor Commented:
Great resource for getting things done like I need to do!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.