2013-07-18 47 views
0

我在中有一個TextBox在WPF數據網格中。它不會繼承數據網格本身的外觀。例如,當選擇或編輯行時,它不顯示交替的顏色。如何在wpf datagrid中繼承默認的文本框樣式?

 <DataGridTemplateColumn> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBox Text="{Binding ...}" />       
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 

它看起來像一個默認的文本框的樣式將覆蓋該DataGrid。就是有什麼辦法可以使用DataGrid的風格?

+1

默認情況下,「TextBox」的風格是什麼?你的風格或系統? –

+0

我沒有undrestand你是什麼意思,DataGridTemplateColumn中的文本框應該與DataGridTextColumn中的文本框具有相同的樣式。 –

+0

不,它不應該。當你在'DataTemplate'中指定一個強制的'TextBox'時,你會自動覆蓋'DataGrid'的默認'DataGridCell'風格。您需要爲'DataGridCell'建立單一樣式,那麼'TextBox'(在'DataGridTemplateColumn'和'DataGridTextColumn'中)將是所有樣式。爲DataGridCell設置統一樣式時,它仍然需要爲DataGridRow設置樣式,因爲選擇該行將不起作用。在任何情況下,最好爲DataGrid採用預定義的樣式併爲自己重新制作樣式。 –

回答

0

以防萬一: -

<DataGrid Background="White" AlternatingRowBackground="#BCD2EE" 
     <DataGrid.CellStyle> 
          <Style TargetType="DataGridCell"> 
           <Style.Triggers> 
            <Trigger Property="IsSelected" Value="True"> 
             <Setter Property="BorderBrush" Value="Transparent" /> 
             <Setter Property="Background" Value="Transparent" /> 
            </Trigger> 
           </Style.Triggers> 
          </Style> 
         </DataGrid.CellStyle> 
.... 
..... 
.... 
    </DataGrid> 

這是我用過一次,你可以根據你的要求設置屬性。

This Might help .. :)