样式化 WPF 布局网格背景(每个单元格、行、列)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/565243/
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
Styling a WPF layout grid background (of each cell, row, column)
提问by Mladen Mihajlovic
I would like to know if there is any way to style a WPF layout grid's cells, rows and columns. I've been trying to find any information and the few mentions I've found have not been that informative.
我想知道是否有任何方法可以设置 WPF 布局网格的单元格、行和列的样式。我一直在努力寻找任何信息,但我发现的少数提及并没有那么丰富。
I would like to style the grid to look like the one in the linked screenshot.
我想将网格样式设置为链接屏幕截图中的样式。
If the actual control does not support it, can I inherit it somehow and do it then? I am quite new to WPF so any help would be very appreciated.
如果实际控件不支持它,我可以以某种方式继承它然后再做吗?我对 WPF 很陌生,因此非常感谢任何帮助。
One other thing, I know I can style each and every control within the grid, but it seems like overkill. I would like to have a grid that does it itself.
另一件事,我知道我可以为网格中的每个控件设置样式,但这似乎有点矫枉过正。我想要一个自己做的网格。
screenshot http://img21.imageshack.us/img21/2842/capturehz8.png
采纳答案by Pete OHanlon
Here's a quick (very rough sample) that you could hack around to get the format you want (if you're serious about working with WPF, you'll find Blend an enormous help in getting your layouts looking good):
这是一个快速(非常粗略的示例),您可以对其进行修改以获得所需的格式(如果您认真对待使用 WPF,您会发现 Blend 对让您的布局看起来不错有很大帮助):
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="CustomerDefinition" TargetType="TextBlock">
<Setter Property="Control.FontFamily" Value="Tahoma"/>
<Setter Property="Control.FontSize" Value="12"/>
<Setter Property="Control.Foreground" Value="Red"/>
</Style>
<Style TargetType="{x:Type Label}">
<Setter Property="Width" Value="100"/>
</Style>
<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="MinWidth" Value="120"/>
<Setter Property="MinHeight" Value="20"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="Width" Value="200"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border
Name="Border"
Background="#FFEBE9E9"
BorderBrush="#FF8B8787"
BorderThickness="1"
CornerRadius="2"
Padding="3">
<ScrollViewer x:Name="PART_ContentHost" Margin="0"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background"
Value="#EEEEEE"/>
<Setter TargetName="Border" Property="BorderBrush"
Value="#EEEEEE"/>
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0.0" Color="#FFF0EDED"/>
<GradientStop Offset="1.0" Color="#FFE1E0E0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
</Page.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="26"/>
<RowDefinition Height="23"/>
<RowDefinition Height="24"/>
<RowDefinition Height="24"/>
<RowDefinition Height="24"/>
</Grid.RowDefinitions>
<TextBlock
Grid.ColumnSpan="2"
Grid.Row="0"
Style="{StaticResource CustomerDefinition}"
Text="Customer Definition"/>
<Border
Grid.Column="0"
Grid.Row="1"
Background="#FFEBE9E9"
BorderBrush="#FF8B8787"
BorderThickness="1">
<StackPanel Background="{StaticResource NormalBrush}" Orientation="Horizontal">
<Label Content="Customer Code"/>
<TextBox Text="SMITHA 098 (normally I'd bind here)"/>
</StackPanel>
</Border>
<Border
Grid.Column="1"
Grid.Row="1"
Background="#FFEBE9E9"
BorderBrush="#FF8B8787"
BorderThickness="1">
<StackPanel Background="{StaticResource NormalBrush}" Orientation="Horizontal">
<Label Content="Customer Type"/>
<TextBox Text="PRIVATE INDIVIDUAL"/>
</StackPanel>
</Border>
</Grid> </Page>
回答by Vegar
@Dan recommends WPF Unleashed, which I'm currently reading. Just this morning, I come across a section addressing your question.
@Dan 推荐 WPF Unleashed,我目前正在阅读。就在今天早上,我看到了一个部分来解决您的问题。
Chapter 6, Page 161:
第 6 章,第 161 页:
FAQ: How can I give Grid cells background colors, padding, and borders like I can with cells of a HTML Table?
There is no intrinsic mechanism to give Grid cells such properties, but you can simulate them pretty easily thanks to the fact that multiple elements can appear in any Grid cell. To give a cell a background color, you can simply plop in a Rectangle with the appropriate Fill, which stretches to fill the cell by default. To give a cell padding, you can use auto sizing and set the Margin on the appropriate child element. For borders, you can again use a Rectangle but give it an explicit Stroke of the appropriate color, or you can simply use a Border element instead.
Just be sure to add such Rectangles or Borders to the Grid before any of the other children (or explicitly mark them with the ZIndex attached property), so their Z order puts them behind the main content.
常见问题解答:如何像 HTML 表格的单元格一样为网格单元格提供背景颜色、填充和边框?
没有给 Grid 单元格提供这样的属性的内在机制,但是由于多个元素可以出现在任何 Grid 单元格中的事实,您可以很容易地模拟它们。要为单元格提供背景颜色,您可以简单地插入一个带有适当填充的矩形,默认情况下它会拉伸以填充单元格。要提供单元格填充,您可以使用自动调整大小并在适当的子元素上设置边距。对于边框,您可以再次使用 Rectangle 但为其指定适当颜色的显式 Stroke,或者您可以简单地使用 Border 元素。
只要确保在任何其他子项之前将此类 Rectangles 或 Borders 添加到 Grid(或使用 ZIndex 附加属性明确标记它们),因此它们的 Z 顺序将它们放在主要内容之后。
Btw, WPF Unleashed rocks. Its very well written, and the print in full color makes it even more easier to read.
顺便说一句,WPF 释放了岩石。它写得很好,全彩色印刷使它更容易阅读。
回答by Drew Noakes
The WPF Grid
doesn't have visible cells as such. Think of them as invisible grid lines against which you can have child elements be aligned.
WPFGrid
本身没有可见的单元格。将它们视为不可见的网格线,您可以根据这些网格线对齐子元素。
So, to style the grid's cells, you have to style the items that are aligned inside the grid.
因此,要设置网格单元格的样式,您必须设置在网格内对齐的项目的样式。
It is confusing to think of the Grid
as being anything like a WinForms DataGrid
. I guess its closest WinForms equivalent is the TableLayout
control.
将 theGrid
视为类似于 WinForms 的东西令人困惑DataGrid
。我猜它最接近的 WinForms 等价物是TableLayout
控件。
Check out some 3rd party grid controls. I used the DevExpress one while it was in beta and found it pretty straightforward.
查看一些 3rd 方网格控件。我在测试版时使用了 DevExpress,发现它非常简单。
回答by Dan
I would recommend using borders for your styling.
我建议为您的样式使用边框。
You could recreate that layout pretty easily by creating borders for each row and each column and set the rowspans and colspans accordingly.
您可以通过为每一行和每一列创建边框并相应地设置行跨度和列跨度来轻松地重新创建该布局。
You will have 5 borders with colspan 2, these borders will take care of your gradient backgrounds for each row and the borders along the top and bottom of each row. Then you will have 2 borders with rowspan 5 these will handle the column borders. Imagine that you are overlaying the borders to form the visual grid effect you are after.
您将有 5 个带有 colspan 2 的边框,这些边框将处理每行的渐变背景以及每行顶部和底部的边框。然后你将有 2 个边框,rowspan 5 这些将处理列边框。想象一下,您正在叠加边框以形成您所追求的视觉网格效果。
For the header and outer border, just wrap the entire grid with a border and style as needed.
对于标题和外边框,只需根据需要用边框和样式包裹整个网格。
I would recommend storing your styles as resources so you can keep all your styling info in one place.
我建议将您的样式存储为资源,以便您可以将所有样式信息保存在一个地方。
Take care to learn how the styling works because it is pretty powerful, but there is a learning curve as it is quite different to the way CSS works. I would recommend reading WPF Unleashedif you can.
请注意了解样式的工作原理,因为它非常强大,但有一个学习曲线,因为它与 CSS 的工作方式完全不同。如果可以,我建议您阅读WPF Unleashed。
回答by greenoldman
I found this post when looking for method for setting margin (or padding) for DataGrid cells. My problem was solved thanks to example xaml code posted at (near the end) -- pretty minimalistic.
我在寻找为 DataGrid 单元格设置边距(或填充)的方法时发现了这篇文章。由于发布在(接近尾声)的示例 xaml 代码,我的问题得到了解决——非常简约。