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

  • SEARCH
  • Home
  • 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 4274210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:48:59+00:00 2026-05-21T07:48:59+00:00

I have a String to Brush converter that I use in WPF to color

  • 0

I have a String to Brush converter that I use in WPF to color Listbox items based on text content.

However, as you may already know, the listbox behaves buggy when more string items with the same text are inside and you begin clicking casually you can get more selected items (visually).

To solve this problem I created a class OutputMessage that I implicitly use as a string, but since implicit conversion creates new instances of OutputMessage for each string it converts now the listbox compares instances and not string values and behaves correctly.

But I left the logging manager put plain strings in there since it always prefixes with the current time and there are no duplicates, so the problem would not occur.

But I found a wierd situation with unboxing in the converter:

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            String txt = value is OutputMessage ? (OutputMessage)value : (String)value;

By doing this you get:

Error 1 Type of conditional expression cannot be determined because ‘OutputMessage’ and ‘string’ implicitly convert to one another

We are asigning to a String and there is an implicit conversion in any of the cases, so I tried to use the conditional expression to choose a ‘path’ for unboxing the value correctly.

The problem here is that I cannot use

String txt = (String)value;

or

String txt = (OutputMessage)value;

because the listbox item could be any of the two, and unboxing the value from object will fail.

I ultimately solved the problem with if statements and type checking, but that kinda kills the usefulness of implicit conversion. I hoped that conditional expressions coudl deal with this 🙁

  • 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-21T07:49:00+00:00Added an answer on May 21, 2026 at 7:49 am

    You can do:

    String txt = value is OutputMessage ? (String)(OutputMessage)value : (String)value;
    

    Alternately, you can modify OutputMessage so that one of the conversions is explicit rather than implicit (i.e. allow String to be implicitly converted to OutputMessage, but require OutputMessages to be explicitly converted to String.

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

Sidebar

Related Questions

I have an application that allows the user to change color schemes based on
I have a converter that takes the text from a TextBlock that makes up
I have made my own custom converter which given a string returns a Brush
I have string that look like Array that fetched from other webservice like this
I have string that displays UTF-8 encoded characters, and I want to convert it
I have string. I then split the string to make an array. I use
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
i have string stored in python variables, and i am outputting a html that
I have the following .ashx page that takes some query string parameters and returns
I have string separated by dot ('.') characters that represents a hierarchy: string source

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.