This is a migrated thread and some comments may be shown as answers.

Boolean column with a converter

2 Answers 262 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Patrick
Top achievements
Rank 2
Iron
Iron
Iron
Patrick asked on 05 May 2014, 03:28 PM
Hello,
I have a GridView that contains a column bound to a Boolean value.
I want that the value is not displayed with the default check box, so I wrote a converter that displays a check mark only when the value is true.

If I use the converter only on the DataContext, the column is still displayed with the standard check box:
DataContext="{Binding IsTeamMate, Converter={StaticResource BooleanToCheckMark}}"

To have the column displayed normally, I had to define a cell template:
<tk:GridViewDataColumn DataContext="{Binding IsTeamMate, Converter={StaticResource BooleanToCheckMark}}"
                       Header="Already played"
                       IsGroupable="False"
                       ShowFieldFilters="False"
                       UniqueName="IsTeamMate"
                       Width="30">
  <tk:GridViewDataColumn.CellTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding IsTeamMate, Converter={StaticResource BooleanToCheckMark}}" />
    </DataTemplate>
  </tk:GridViewDataColumn.CellTemplate>
</tk:GridViewDataColumn>

Is it a normal behavior or is there another way to display the column correctly ?

2 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 06 May 2014, 01:22 PM
Hi,

By default when the bound value is of type Boolean, a CheckBox will be created as cell element.
How have you defined the Converter you are referring to?

I tested with the following code in the Convert method:
public partial class BooleanToCheckMark : IValueConverter
{
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         if (!value)
         {
             return "False";
         }
         return true ;
     }
 ...

As a result I got some of the cells with a CheckBox marked as true and some cells with text "False".

Basically you need to return the original Boolean value if it is true and a string otherwise.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Patrick
Top achievements
Rank 2
Iron
Iron
Iron
answered on 06 May 2014, 01:50 PM
Hi Didie,
thank you for the answer.
My converter returns either an empty string (for false) or a string containing "✔" (for true).
So this should work.
Tags
GridView
Asked by
Patrick
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dimitrina
Telerik team
Patrick
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or