Click here to Skip to main content
14,771,593 members
Home » Discussions » Silverlight / WPF
   

Silverlight / WPF

 
QuestionComboBox Items Not Showing Up Pin
Kevin Marois7-Aug-17 6:28
professionalKevin Marois7-Aug-17 6:28 
AnswerRe: ComboBox Items Not Showing Up Pin
Mycroft Holmes7-Aug-17 16:22
professionalMycroft Holmes7-Aug-17 16:22 
GeneralRe: ComboBox Items Not Showing Up Pin
Kevin Marois8-Aug-17 5:51
professionalKevin Marois8-Aug-17 5:51 
GeneralRe: ComboBox Items Not Showing Up Pin
Kevin Marois8-Aug-17 6:33
professionalKevin Marois8-Aug-17 6:33 
GeneralRe: ComboBox Items Not Showing Up Pin
Mycroft Holmes8-Aug-17 14:57
professionalMycroft Holmes8-Aug-17 14:57 
QuestionListBox Style Problem Pin
Kevin Marois27-Jul-17 7:53
professionalKevin Marois27-Jul-17 7:53 
AnswerRe: ListBox Style Problem Pin
Henrik Jonsson7-Aug-17 10:35
MemberHenrik Jonsson7-Aug-17 10:35 
QuestionStyle Not being Applied Pin
Kevin Marois24-Jul-17 8:43
professionalKevin Marois24-Jul-17 8:43 
I'm trying to create a simple Watermark TextBox. I first created a TextBox class with a WatermarkText property:
public class MaroisTextBox : TextBox
{
    #region DP WatermarkText
    public static readonly DependencyProperty WatermarkTextProperty =
                DependencyProperty.Register("WatermarkText",
                typeof(string),
                typeof(MaroisTextBox),
                new PropertyMetadata("", new PropertyChangedCallback(OnWatermarkTextChanged)));

    public string WatermarkText
    {
        get { return (string)GetValue(WatermarkTextProperty); }
        set { SetValue(WatermarkTextProperty, value); }
    }

    private static void OnWatermarkTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MaroisTextBox control = (MaroisTextBox)d;

        control.SetWatermark();
    }
    #endregion

    #region Overrides
    protected override void OnGotFocus(RoutedEventArgs e)
    {
        base.OnGotFocus(e);

        SetWatermark();
    }

    protected override void OnLostFocus(RoutedEventArgs e)
    {
        base.OnLostFocus(e);

        SetWatermark();
    }
    #endregion

    private void SetWatermark()
    {
        if (IsFocused)
        {
            // If the watermark is showing, remove it
            if (Text.CompareTo(WatermarkText) == 0)
            {
                Text = string.Empty;
            }
        }
        else
        {
            // If the Text property is empty then appply the watermark
            if (string.IsNullOrEmpty(Text))
            {
                Text = WatermarkText;
            }
        }
    }
}

Next I created a style for it
<Style x:Key="SearchTextBoxStyle"
        TargetType="TextBox">

<pre>
<Setter Property="FontFamily" Value="{StaticResource AppFontName}"/>
<Setter Property="FontSize" Value="{StaticResource NormalFontSize}"/>
<Setter Property="FontStyle" Value="Normal"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="Height" Value="30"/>

<Style.Triggers>
    <Trigger Property="IsFocused" Value="True">

        <Setter Property="Foreground" Value="Red"/>
        <Setter Property="FontStyle" Value="Italic"/>

    </Trigger>
</Style.Triggers>




And its usage
<MaroisTextBox Grid.Column="3"
                    Width="200"
                    Margin="20,2,2,2"
                    Text="{Binding SearchText}"
                    WatermarkText="Search..."
                    Style="{StaticResource SearchTextBoxStyle}"/>

When I click into the textbox, the code behind runs as expected. Yet I don't see anything in the textbox.

When I click out, the watermark text appears, but in black and not italic.

I don't see why this won't work. Anyone see what's wrong here?

Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: Style Not being Applied Pin
Richard Deeming24-Jul-17 8:54
mveRichard Deeming24-Jul-17 8:54 
GeneralRe: Style Not being Applied Pin
Kevin Marois24-Jul-17 11:17
professionalKevin Marois24-Jul-17 11:17 
GeneralRe: Style Not being Applied Pin
Richard Deeming25-Jul-17 1:37
mveRichard Deeming25-Jul-17 1:37 
GeneralRe: Style Not being Applied Pin
Kevin Marois25-Jul-17 5:36
professionalKevin Marois25-Jul-17 5:36 
QuestionHandle DataGrid Row Model Property Change Pin
Kevin Marois21-Jun-17 13:45
professionalKevin Marois21-Jun-17 13:45 
AnswerRe: Handle DataGrid Row Model Property Change Pin
Mycroft Holmes21-Jun-17 21:54
professionalMycroft Holmes21-Jun-17 21:54 
AnswerRe: Handle DataGrid Row Model Property Change Pin
Pete O'Hanlon21-Jun-17 22:46
mvePete O'Hanlon21-Jun-17 22:46 
GeneralRe: Handle DataGrid Row Model Property Change Pin
Kevin Marois22-Jun-17 5:01
professionalKevin Marois22-Jun-17 5:01 
GeneralRe: Handle DataGrid Row Model Property Change Pin
Pete O'Hanlon22-Jun-17 6:03
mvePete O'Hanlon22-Jun-17 6:03 
GeneralRe: Handle DataGrid Row Model Property Change Pin
Mycroft Holmes22-Jun-17 15:41
professionalMycroft Holmes22-Jun-17 15:41 
GeneralRe: Handle DataGrid Row Model Property Change Pin
Pete O'Hanlon22-Jun-17 22:18
mvePete O'Hanlon22-Jun-17 22:18 
GeneralStop user from killing of a running wpf application from background processes in task manager. Pin
Naushad Ansari16-Jun-17 0:24
MemberNaushad Ansari16-Jun-17 0:24 
GeneralRe: Stop user from killing of a running wpf application from background processes in task manager. Pin
Pete O'Hanlon16-Jun-17 0:46
mvePete O'Hanlon16-Jun-17 0:46 
QuestionBind To DP On Control Pin
Kevin Marois15-Jun-17 6:26
professionalKevin Marois15-Jun-17 6:26 
AnswerRe: Bind To DP On Control Pin
Pete O'Hanlon15-Jun-17 7:35
mvePete O'Hanlon15-Jun-17 7:35 
GeneralRe: Bind To DP On Control Pin
Kevin Marois15-Jun-17 7:39
professionalKevin Marois15-Jun-17 7:39 
GeneralRe: Bind To DP On Control Pin
Richard Deeming15-Jun-17 7:58
mveRichard Deeming15-Jun-17 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.