Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3458380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:58:33+00:00 2026-05-18T09:58:33+00:00

I have a text box for user to enter 6 character hex color value,

  • 0

I have a text box for user to enter 6 character hex color value, a validator & a converter attached to it. Up to here, everything works fine. But I want to bind a textbox’s Background color to the color specified by the text box (ElementNames Background & Foreground), and it does not seem to work.

When I debug/step through the code, the value seems like is always ""

XAML

<TextBox x:Name="Background" Canvas.Left="328" Canvas.Top="33" Height="23" Width="60">
    <TextBox.Text>
        <Binding Path="Background">
            <Binding.ValidationRules>
                <validators:ColorValidator Property="Background" />
            </Binding.ValidationRules>
            <Binding.Converter>
                <converters:ColorConverter />
            </Binding.Converter>
        </Binding>
    </TextBox.Text>
</TextBox>
<TextBlock Canvas.Left="403" Canvas.Top="12" Text="Foreground" />
<TextBox x:Name="Foreground" Canvas.Left="403" Canvas.Top="33" Height="23" Width="60">
    <TextBox.Text>
        <Binding Path="Foreground">
            <Binding.ValidationRules>
                <validators:ColorValidator Property="Foreground" />
            </Binding.ValidationRules>
            <Binding.Converter>
                <converters:ColorConverter />
            </Binding.Converter>
        </Binding>
    </TextBox.Text>
</TextBox>

<!-- in this example I used the converter used in the TextBox & another converter that converts a string to color -->
<TextBox ... 
    Background="{Binding ElementName=Background, Path=Text, Converter={StaticResource colorConverter}}" 
    Foreground="{Binding ElementName=Foreground, Path=Text, Converter={StaticResource stringToColorConverter}}" />

Converters

class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            string entry = ((Color)value).ToString();
            return entry.Substring(3); 
        } catch (Exception) {
            return Binding.DoNothing;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string entry = (string)value;
        Validators.ColorValidator validator = new Validators.ColorValidator();
        if (!validator.Validate(entry, System.Globalization.CultureInfo.CurrentCulture).IsValid) {
            return Binding.DoNothing;
        }
        return (Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF" + entry);
    }
}

class StringToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string entry = (string)value;
        Validators.ColorValidator validator = new Validators.ColorValidator();
        if (!validator.Validate(entry, System.Globalization.CultureInfo.CurrentCulture).IsValid)
        {
            return Binding.DoNothing;
        }
        return (Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF" + entry);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:58:33+00:00Added an answer on May 18, 2026 at 9:58 am

    Everyone saying that you need a brush and not a color is right.

    Solution: Create another converter that returns a SolidColorBrush and you will be golden.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one text box when user enter in text box and hit enter
I have a text box that will allow the user to enter multiple authors
In my web application, I have a text box. The user should enter a
I have a text box , and user enters text on here , I
i have text box in that user have to enter only text. user shouldn't
I have a text-box which allows users to enter a word. The user enters:
I have a text box in which a user is supposed to enter a
I have a text field in which user can enter any character he/she wants.
In my web application, I have text box where the user should enter characters
Have a text input box. Would like the user to enter a string in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.