Willy
Willy

Reputation: 10660

Set textbox text to 'Yes' or 'No' based on boolean property within a DataGridTextColumn

I have a datagrid and one of my column is like below:

<DataGridTextColumn Binding="{Binding Path=BoolPropertyValue}" Header="YesOrNo" HeaderStyle="{DynamicResource MyHeaderStyle}" Width="auto">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="Margin" Value="3 5"/>
            <Setter Property="Text" Value="No"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=BoolPropertyValue}" Value="True">
                    <Setter Property="Text" Value="Yes"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

What I am trying to do is to put 'Yes' or 'No' in the column for that row when BoolPropertyValue is true or false respectively.

BoolPropertyValue is a boolean property.

Unfortunately this is not working, it always puts 'True' or 'False'.

I would like to do this without the need of a converter and only using xaml code.

What am I doing wrong?

Upvotes: 1

Views: 2469

Answers (2)

Bradley Uffner
Bradley Uffner

Reputation: 17001

I would use an IValueConverter instead of triggers (this can be done with triggers if you really want to, it's a personal preference):

BoolToYesNoConverter.cs:

public class BoolToYesNoConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
    {
        return (bool)value ? "Yes" : "No";
    }

    public object ConvertBack(object value, Type targetType,
           object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

<DataGridTextColumn Binding="{Binding Path=BoolPropertyValue, Converter={StaticResource BoolToYesNoConverter}}" Header="YesOrNo" HeaderStyle="{DynamicResource MyHeaderStyle}" Width="auto">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="Margin" Value="3 5"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

Somewhere in a resource (probably in App.xaml):

<local:BoolToYesNoConverter x:key="BoolToYesNoConverter" />

Upvotes: 2

The One
The One

Reputation: 4706

Or you can modify the cell template...

<DataGridTemplateColumn Header="YesOrNo" HeaderStyle="{DynamicResource MyHeaderStyle}" Width="auto">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBlock>
        <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
         <Setter Property="HorizontalAlignment" Value="Left" />
         <Setter Property="Margin" Value="3 5"/>
         <Style.Triggers>
            <DataTrigger Binding="{Binding Path=BoolPropertyValue}" Value="True">
                <Setter Property="Text" Value="Yes"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=BoolPropertyValue}" Value="False">
                <Setter Property="Text" Value="No"/>
            </DataTrigger>
         </Style.Triggers>
        </Style>
        </TextBlock.Style>
      </TextBlock>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn >

Upvotes: 0

Related Questions