WPF: Display a bool value as “Yes” / “No”

后端 未结 7 1930
盖世英雄少女心
盖世英雄少女心 2020-11-30 00:21

I have a bool value that I need to display as \"Yes\" or \"No\" in a TextBlock. I am trying to do this with a StringFormat, but my StringFormat is ignored and the TextBlock

7条回答
  •  青春惊慌失措
    2020-11-30 01:04

    The following worked for me inside a datagridtextcolumn: I added another property to my class that returned a string depending on the value of MyBool. Note that in my case the datagrid was bound to a CollectionViewSource of MyClass objects.

    C#:

    public class MyClass        
    {     
        public bool MyBool {get; set;}   
    
        public string BoolString    
        {    
            get { return MyBool == true ? "Yes" : "No"; }    
        }    
    }           
    

    XAML:

    
    

提交回复
热议问题