在 WPF 中的 dataGridCells 上设置填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5246745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set a padding on dataGridCells in WPF
提问by David
simple question: How can I set a padding on a dataGridCell in WPF? (either one at a time or on all cells, I don't care)
简单的问题:如何在 WPF 中的 dataGridCell 上设置填充?(一次一个或在所有单元格上,我不在乎)
I have tried using the DataGrid.CellStyle
property by adding a setter on the DataGridCell.Padding
property as well as using the DataGridColumn.CellStyle
property in the same way with no effect.
我已经尝试通过在DataGrid.CellStyle
属性上添加一个 setterDataGridCell.Padding
以及以DataGridColumn.CellStyle
相同的方式使用该属性而没有效果来使用该属性。
I also tried using the DataGridColumn.ElementStyle
property with no more luck.
我也尝试使用该DataGridColumn.ElementStyle
属性,但没有更多运气。
I'm kind of stuck there, has anyone managed to get a padding applied on a dataGridCell?
我有点卡在那里,有没有人设法在 dataGridCell 上应用填充?
NB: I'll add that no, I cannot use transparent borders to do this, since I already use the border properties for something else. I also cannot use the margin property (which seems to work, surprisingly enough) as I use the background property and I don't want any "blank" space between my cells.
注意:我要补充一点,不,我不能使用透明边框来执行此操作,因为我已经将边框属性用于其他用途。我也不能使用 margin 属性(这似乎有效,令人惊讶的是),因为我使用了 background 属性并且我不希望我的单元格之间有任何“空白”空间。
回答by Fredrik Hedblad
The problem is that the Padding
isn't transfered to the Border
that's in the Template for DataGridCell
. You can edit the Template and add the TemplateBinding for Padding
问题是Padding
没有转移到Border
模板中的DataGridCell
. 您可以编辑模板并添加模板绑定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>
回答by Scott Brickey
Here's a cleaner method (my opinion) that combines the approach from David
这是一个更简洁的方法(我的意见),它结合了 David 的方法
<Resources>
<Style x:Key="ColumnElementStyle" TargetType="TextBlock">
<Setter Property="Margin" Value="5,0,10,0" />
</Style>
</Resources>
then...
然后...
<DataGridTextColumn ElementStyle="{StaticResource ColumnElementStyle}" />
<DataGridTextColumn ElementStyle="{StaticResource ColumnElementStyle}" />
(in my case, my rows are readonly, so no EditingStyle)
(在我的情况下,我的行是只读的,所以没有 EditingStyle)
回答by David
Almost 5 years later, since this question seems to still be of use (it is still getting upvotes) and since it has been requested, here is the solution I used (with the ElementStyle) on a TextColumn (but you can do the same for any type of DataGridColumn):
差不多 5 年后,由于这个问题似乎仍然有用(它仍然得到了投票)并且因为它已被请求,这里是我在 TextColumn 上使用的解决方案(使用 ElementStyle)(但您可以为任何类型的 DataGridColumn):
I did it all in code behind:
我在后面的代码中完成了这一切:
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)));
}
}
But if you want to do it directly in xaml:
但是如果你想直接在 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>
回答by Raj
<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>
回答by Jacob
You could also try changing
你也可以尝试改变
{Binding BindingValue, StringFormat={}{0:#0.0000}}
to
到
{Binding BindingValue, StringFormat={}{0:#0.0000 }}
Interestingly enough WPF's XAML {0:#0.0000 } will honor this extra space character in the format of the rendered control to move your values off of the edge of your grid columns.
有趣的是,WPF 的 XAML {0:#0.0000} 将以呈现控件的格式尊重这个额外的空格字符,以将您的值从网格列的边缘移开。