Click here to Skip to main content
14,453,795 members
Rate this:
Please Sign up or sign in to vote.
See more: , +
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.

      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 21:34pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100