带样式的DataGrid数据绑定问题

时间:2011-09-15 10:23:22

标签: wpf data-binding datagrid

我有一个WPF DataGrid,我想对所有单元格应用文本换行,所以我已经定义了这种风格:

        <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>

我在我的代码中设置了DataGrid的ItemsSource,如下所示:

        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"}
        };

但某些地方的某些东西因我的样式Text="{Binding}"设置的绑定表达式而失败,我最终得到了:

enter image description here

显然绑定表达式Text="{Binding}"失败了,我知道这是因为当我删除样式时,一切都很完美。我该如何解决这个问题?

提前致谢。

2 个答案:

答案 0 :(得分:2)

设置DataGridColumn.Binding属性不会为每个DataContext设置DataGridCellDataContext仍然等于整个行的DataContext

从绑定ContentTemplate切换到绑定Template,然后您就可以访问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>

答案 1 :(得分:1)

这会有所帮助:

<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>

取自here