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 6667979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:59:26+00:00 2026-05-26T02:59:26+00:00

I just made my first converter to convert from int to string. I have

  • 0

I just made my first converter to convert from int to string. I have a combobox fill with integers(years) but if the value is 0 I want the combobox to show ‘All’.

This is my converter:

public class IntToString : IValueConverter
{
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                int intY = (int)value;

                if (intY == 0)
                {
                    String strY = "All";
                    return strY;
                }
                else
                {
                    return intY.ToString();
                }
            }

            return String.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

        }
    }

In XAML where should I set the converter ? I tried in the ItemsSource of the combobox:

ItemsSource="{Binding YearsCollection, Converter={StaticResource intToStringYearConverter}}"

But I always get InvalidcastException on this line:

int intY = (int)value;
  • 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-26T02:59:26+00:00Added an answer on May 26, 2026 at 2:59 am

    The problem is that you are trying to convert the entire collection rather than just one item from the collection.

    You would want to do something like this:

    <ListBox ItemsSource="{Binding YearsCollection}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                 <Border DataContext="{Binding Converter={StaticResource intToStringYearConverter}">
                 ...
                 </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just made my first proper little desktop GUI application that basically wraps
I just made my first steps moving into Objective-C . I have a very
I just made a register script from scratch, but I'm still going to improve
I have a package that I just made and I have an old-mode that
Having made the jump just recently from XEmacs to GNU Emacs, I'm really loving
So i just made my first RSS feed, or so i tought, in combination
I just got a fix on another post I just made but this created
i am new to java and i just made my first program: HelloWorld and
I just made a fresh copy of eclipse and installed pydev. In my first
I'm just starting Java ... again. I just made a simple program class first

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.