ms87
ms87

Reputation: 17492

DataGrid Data Binding Issue with Styles

I have a WPF DataGrid and I want to apply text wrapping to all the cells, so I've defined this style:

        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock TextWrapping="Wrap" FontSize="15" Text="{Binding}"  VerticalAlignment="Center" 
                                 HorizontalAlignment="Center" ></TextBlock>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.Columns>

            <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="95" />
            <DataGridTextColumn Header="Address" Binding="{Binding Address}" Width="95"  />
            <DataGridTextColumn Header="Category" Binding="{Binding Category}" Width="95" />

        </DataGrid.Columns>

and I set my DataGrid's ItemsSource in my code behind like this:

        myDataGrid.ItemsSource= new Customers[] 
        { 
                new ComputerStandard{Name="Michael Thomas",Address="16 Greenville Avenue",Category="A"},            
                new ComputerStandard{Name="Fiona Thompson",Address="19 Wright Street",Category="F"},            
                new ComputerStandard{Name="Jack Smith",Address="133 Kensington Road",Category="B"},
                new ComputerStandard{Name="Michael jackson",Address="11 Wine Street",Category="C"},
                new ComputerStandard{Name="Jerry Holmes",Address="10 Awson Street",Category="G"},
                new ComputerStandard{Name="David Philips",Address="Not Specified",Category="A"}
        };

But somewhere something fails with my binding expression that I set in my style Text="{Binding}" and I end up with:

enter image description here

Obviously the binding expression Text="{Binding}" is failing, I know this because when I remove the style everything works perfectly. How do I go about fixing this?

Thanks in advance.

Upvotes: 3

Views: 1699

Answers (2)

Rachel
Rachel

Reputation: 132558

Setting the DataGridColumn.Binding property does not set the DataContext for each DataGridCell. The DataContext is still equal to the entire Row's DataContext

Switch from binding the ContentTemplate, to binding the Template, and then you have access to the ContentPresenter

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="DataGridCell">
            <TextBlock TextWrapping="Wrap" FontSize="15" 
                       VerticalAlignment="Center" HorizontalAlignment="Center">

                <ContentPresenter Content="{TemplateBinding Content}" />

            </TextBlock>
        </ControlTemplate>
    </Setter.Value>
</Setter>

Upvotes: 2

loxxy
loxxy

Reputation: 13151

This will help :

<Style x:Key="MyGrid" TargetType="{x:Type DataGridCell}">    
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
               <Border x:Name="MyBorder" >
                  <TextBlock Background="Transparent" TextWrapping="WrapWithOverflow"
                     TextTrimming="CharacterEllipsis" Height="auto" Width="auto"> 
                   <ContentPresenter 
                     Content="{TemplateBinding Property=ContentControl.Content}" 
                     ContentTemplate="{TemplateBinding Property=ContentControl.Content}"/>
                  </TextBlock>
               </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Taken from here.

Upvotes: 1

Related Questions