2011-03-09 52 views
48

簡單的問題:如何在WPF中的dataGridCell上設置填充? (或一次一個或所有細胞,我不在乎)在WPF中的dataGridCells上設置填充

我已經加入對DataGridCell.Padding屬性的設置,以及在沒有以同樣的方式使用DataGridColumn.CellStyle財產使用DataGrid.CellStyle財產嘗試影響。

我也試過使用DataGridColumn.ElementStyle屬性,沒有更多的運氣。

我有點卡在那裏,有人設法得到一個填充應用於dataGridCell?

注意:我會補充說不,我不能使用透明邊框來做到這一點,因爲我已經使用了其他的邊框屬性。我也不能使用margin屬性(這似乎工作,令人驚訝的足夠),因爲我使用背景屬性,我不希望我的單元格之間有任何「空白」空間。

回答

92

問題是Padding沒有被轉移到Border那是在DataGridCell的模板中。您可以編輯該模板並添加TemplateBinding爲Padding

<DataGrid ...> 
    <DataGrid.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Setter Property="Padding" Value="20"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type DataGridCell}"> 
         <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
          <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </DataGrid.CellStyle> 
    <!--...--> 
</DataGrid> 
+0

好吧,我接受了你的答案,雖然我找到了另一種方式(多一點黑客,但在我的情況下更容易):在ce上設置保證金屬性通過使用ElementStyle屬性來實現內容。雖然你的解決方案更好。 – David 2011-03-09 14:35:51

+0

@David:是的,這將是另一種方式。無論什麼工作:) – 2011-03-09 14:40:08

+8

我會補充說,這是另一件事,讓我覺得WPF仍然遠遠沒有完成,據我所知... – David 2011-03-09 14:44:42

0

您也可以嘗試改變

{Binding BindingValue, StringFormat={}{0:#0.0000}} 

{Binding BindingValue, StringFormat={}{0:#0.0000 }} 

有趣的是WPF的XAML {0:#0.0000}將榮譽這個額外的空格字符以渲染控件的格式將您的值從網格列的邊緣移開。

6

大約5年後,由於這個問題似乎仍然有用(它仍然是upvotes),並且因爲它已被請求,這裏是我使用的解決方案(與ElementStyle)在TextColumn(但你可以對於任何類型的DataGridColumn的)做同樣的:

我所做的這一切在後面的代碼:

class MyTextColumn : DataGridTextColumn 
{ 
    public MyTextColumn() 
    { 
     ElementStyle = new Style(typeof(TextBlock)); 
     EditingElementStyle = new Style(typeof(TextBox)); 

     ElementStyle.Setters.Add(new Setter(FrameworkElement.MarginProperty, new Thickness(3))); 
     EditingElementStyle.Setters.Add(new Setter(Control.PaddingProperty, new Thickness(0, 1, 0, 1))); 
    } 
} 

但是,如果你想直接在XAML中做到這一點:

<DataGrid.Columns> 
    <DataGridTextColumn> 
     <DataGridTextColumn.ElementStyle> 
      <Style TargetType="TextBlock"> 
       <Setter Property="Margin" Value="3"/> 
      </Style> 
     </DataGridTextColumn.ElementStyle> 
     <DataGridTextColumn.EditingElementStyle> 
      <Style TargetType="TextBox"> 
       <Setter Property="Padding" Value="0 1 0 1"/> 
      </Style> 
     </DataGridTextColumn.EditingElementStyle> 
    </DataGridTextColumn> 
</DataGrid.Columns> 
9

這裏的結合從大衛

<Resources> 
    <Style x:Key="ColumnElementStyle" TargetType="TextBlock"> 
     <Setter Property="Margin" Value="5,0,10,0" /> 
    </Style> 
</Resources> 

那麼方法清潔方法(我認爲)...

<DataGridTextColumn ElementStyle="{StaticResource ColumnElementStyle}" /> 
<DataGridTextColumn ElementStyle="{StaticResource ColumnElementStyle}" /> 

(在我的情況,我行是隻讀的,所以沒有EditingStyle)

0
<DataGrid.Columns> 
     <DataGridTextColumn MinWidth="100" Header="Changed by" Width="" Binding="{Binding Changedby}" IsReadOnly="True" > 
     <DataGridTextColumn.CellStyle> 
      <Style TargetType="DataGridCell"> 
      <Setter Property="BorderThickness" Value="0"/> 
      <Setter Property="Background" Value="Transparent" /> 
     <Setter Property="FrameworkElement.HorizontalAlignment"Value="Center"/> 
      </Style> 
     </DataGridTextColumn.CellStyle> 
    </DataGridTextColumn>