造型WPF布局网格背景(每个单元格,行,列) [英] Styling a WPF layout grid background (of each cell, row, column)

查看:124
本文介绍了造型WPF布局网格背景(每个单元格,行,列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方式来设计WPF布局网格的单元格,行和列。我一直在试图找到任何信息,而且我发现的一些提及的信息都没有提供。



我想让网格看起来像一个如果实际的控件不支持它,我可以继承它,然后做到这一点吗?我对WPF相当陌生,所以任何帮助都将非常值得赞赏。另外一件事,我知道我可以在网格中对每一个控件进行样式设计,但它看起来像矫枉过正。我想有一个网格自己做。



截图http://img21.imageshack.us/img21/2842/capturehz8.png

解决方案

这里有一个快速(非常粗略的示例),你可以使用它来获取你想要的格式(如果你认真对待WPF,你会发现Blend在获得布局看起来不错的帮助):

 < Page xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:x =http:/ /schemas.microsoft.com/winfx/2006/xaml\"> 
< Page.Resources>
< Style x:Key =CustomerDefinitionTargetType =TextBlock>
< Setter Property =Control.FontFamilyValue =Tahoma/>
< Setter Property =Control.FontSizeValue =12/>
< Setter Property =Control.ForegroundValue =Red/>
< / style>
< Style TargetType ={x:Type Label}>
< Setter Property =WidthValue =100/>
< / style>
< Style x:Key ={x:Type TextBox}TargetType ={x:Type TextBox}>
< Setter Property =SnapsToDevicePixelsValue =True/>
< Setter Property =OverridesDefaultStyleValue =True/>
< Setter Property =KeyboardNavigation.TabNavigationValue =None/>
< Setter Property =FocusVisualStyleValue ={x:Null}/>
< Setter Property =MinWidthValue =120/>
< Setter Property =MinHeightValue =20/>
< Setter Property =AllowDropValue =true/>
< Setter Property =WidthValue =200/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type TextBoxBase}>
Border
$ BorderBrush =#FF8B8787
BorderThickness =1
CornerRadius
背景=#FFEBE9E9
背景= =2
Padding =3>
< ScrollViewer x:Name =PART_ContentHostMargin =0/>
< / Border>
< ControlTemplate.Triggers>
<触发属性=IsEnabled值=假>
< Setter TargetName =BorderProperty =Background
Value =#EEEEEE/>
< Setter TargetName =BorderProperty =BorderBrush
Value =#EEEEEE/>
< Setter Property =ForegroundValue =#888888/>
< /触发>
< /ControlTemplate.Triggers>
< / ControlTemplate>
< / Setter>
< / style>
< LinearGradientBrush x:Key =NormalBrushStartPoint =0,0EndPoint =0,1>
< GradientBrush.GradientStops>
< GradientStopCollection>
< GradientStop Offset =0.0Color =#FFF0EDED/>
< GradientStop Offset =1.0Color =#FFE1E0E0/>
< / GradientStopCollection>
< / LinearGradientBrush>
<网格>
< 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
背景=#FFEBE9E9
BorderBrush =#FF8B8787
BorderThickness =1>
< StackPanel Background ={StaticResource NormalBrush}Orientation =Horizo​​ntal>
<标签内容=客户代码/>
< TextBox Text =SMITHA 098(通常我会绑定在这里)/>
< / StackPanel>
< / Border>
< Border
Grid.Column =1
Grid.Row =1
背景=#FFEBE9E9
BorderBrush =#FF8B8787
BorderThickness =1>
< StackPanel Background ={StaticResource NormalBrush}Orientation =Horizo​​ntal>
<标签内容=客户类型/>
< TextBox Text =PRIVATE INDIVIDUAL/>
< / StackPanel>
< / Border>
< / Grid> < /页>


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.

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.

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

解决方案

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):

<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>

这篇关于造型WPF布局网格背景(每个单元格,行,列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆