How to change style for WPF text box and Calender RRS feed

  • Question

  • Hi,

    When I hover my mouse over any text box it shows by default light blue color as border color. How can change this color to any other color?

    Similar behavior occurs when I try to select a date from a calendar or a month it provides blue color as border color by default. Is there any way to customize this default behavior to change the change?

    Monday, September 16, 2019 11:47 AM

All replies

  • Shubham,

    You can get the original template and write it to a file (or I like to just write to a textbox, so I can copy/paste and then see what I can change. I think the blue you are seeing is from a System.Color and changes with theme, so if you like to override it, you can. here is some code to get you started. Put a TextBox and Calendar on a WPF Window and get their template:

    StringBuilder sb = new StringBuilder(); using (TextWriter writer = new StringWriter(sb)) {

    System.Windows.Markup.XamlWriter.Save(myTextBox.ControlTemplate, writer); } textBox1.Text = sb.ToString(); StringBuilder sb2 = new StringBuilder(); using (TextWriter writer = new StringWriter(sb2)) {

    System.Windows.Markup.XamlWriter.Save(myCalendar.ControlTemplate, writer); } textBox1.Text = sb2.ToString();

    Here is code for TextBox:

    <?xml version="1.0" encoding="utf-8" ?>
    <ControlTemplate TargetType="TextBoxBase" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
      <mwt:ListBoxChrome Background="{TemplateBinding Panel.Background}" BorderBrush="{TemplateBinding Border.BorderBrush}" BorderThickness="{TemplateBinding Border.BorderThickness}" RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" RenderFocused="{TemplateBinding UIElement.IsKeyboardFocusWithin}" Name="Bd" SnapsToDevicePixels="True">
        <ScrollViewer Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
        <Trigger Property="UIElement.IsEnabled">
          <Setter Property="Panel.Background" TargetName="Bd">
              <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
          <Setter Property="TextElement.Foreground">
              <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />

    Seems loke you might have to do a TextBoxBase? I think you can look and see if there is code provided by Microsoft also.

    noorbakhsh حميد نوربخش

    • Edited by noorbakhsh Monday, September 16, 2019 4:45 PM
    Monday, September 16, 2019 4:41 PM