2011-09-15 27 views
3

私は、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

DataGridColumn.Bindingプロパティを設定しても、DataGridCellにはDataContextが設定されません。 DataContextはまだTemplateを結合し、ContentTemplateを結合から全体の行のDataContext

スイッチに等しく、その後、あなたは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

これは役立ちます:hereから撮影

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

を。

関連する問題