Click here to Skip to main content
15,885,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,
I have been working on figuring out this problem for quite some time now. I am pulling a user object from a database and displaying it in a listview in WPF C#. I can read the data and I have the data separated in columns. I have a Boolean value(bit) in SQL and in my column it displays True/False. In my listview I want it to display Yes/No instead of True/False.
I tried making a method but I am not sure how to call the method in the listview. I tried making a separate template for my Boolean column but still no luck. I am not sure if this is the way I should be trying to do this or not so any input / feedback would be much appreciated.
Thank you.

C#
      private void PopulateListBox(){
            lstUserList.Items.Clear();
            users = UsersDB.GetUsersList();
            foreach(User user in users){
                lstUserList.Items.Add(user);
            }
        }
        public static string MakeBooleanYesOrNo(this bool value){
            string boolvalue = "";
            if (value.ToString() == "True"){
                boolvalue = "Yes";
            }else{
                boolvalue = "No";
            }
            //return value.ToString();
            return boolvalue;        }


//This is how I display the data In my listview
<GridViewColumn Header="Admin" Width="50px" DisplayMemberBinding="{Binding IsAdmin}"/>


//I was also thinking maybe something like this if I can do this in C# not ASP.net
<ItemTemplate><%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %></ItemTemplate>


What I have tried:

I have tried making my own method and different templates but im not sure how to call my method in a template or if this is the way i should be going with this
Posted
Updated 21-Oct-18 20:34pm

1 solution

implement IValueConverter like this
public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var boo = value != null && (bool)value;
            return boo ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }


Create an instance and use it when you binding data in your xaml file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900