Mla*_*vic 26 wpf grid xaml styles controltemplates
我想知道是否有任何方式来设置WPF布局网格的单元格,行和列的样式.我一直在努力寻找任何信息,而我发现的一些信息并没有提供相关信息.
我想将网格样式设置为链接屏幕截图中的样式.
如果实际控件不支持它,我可以以某种方式继承它然后执行吗?我对WPF很新,所以任何帮助都会非常感激.
另外一件事,我知道我可以为网格中的每个控件设置样式,但这看起来有点矫枉过正.我想有一个自己做的网格.
Veg*_*gar 25
@Dan推荐WPF Unleashed,我正在阅读.就在今天早上,我遇到了一个解决你问题的部分.
第6章,第161页:
常见问题:如何使用HTML表格的单元格为网格单元格提供背景颜色,填充和边框?
没有内在机制为Grid单元格提供这样的属性,但是由于多个元素可以出现在任何Grid单元格中,因此您可以非常轻松地模拟它们.要为单元格提供背景颜色,您可以简单地使用适当的填充来填充矩形,默认情况下会延伸以填充单元格.要提供单元格填充,可以使用自动调整大小并在相应的子元素上设置边距.对于边框,您可以再次使用Rectangle但是为其指定适当颜色的显式Stroke,或者您只需使用Border元素.
只需确保在任何其他子节点之前将这些矩形或边框添加到网格中(或使用ZIndex附加属性明确标记它们),因此它们的Z顺序将它们放在主要内容之后.
顺便说一下,WPF释放了岩石.它写得非常好,全彩色打印使它更容易阅读.
Pet*_*lon 19
这是一个快速(非常粗略的样本),你可以破解以获得你想要的格式(如果你认真使用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>
Run Code Online (Sandbox Code Playgroud)
WPF Grid没有这样的可见单元格.将它们视为不可见的网格线,您可以将子元素对齐.
因此,要为网格的单元格设置样式,您必须设置在网格内对齐的项目的样式.
将其Grid视为类似WinForms的东西令人困惑DataGrid.我猜它最接近的WinForms等价物是TableLayout控件.
查看一些第三方网格控件.我在测试版中使用了DevExpress,发现它很简单.
| 归档时间: |
|
| 查看次数: |
88993 次 |
| 最近记录: |