首页 新闻 会员 周边 捐助

WPF中DataGrid行数据居中问题

1
悬赏园豆:100 [已解决问题] 解决于 2012-03-21 09:44

各位WPF高手,我在我的前台页面中绑定了datagrid的样式,但是不论怎么改,行数据都无法居中,请问是什么原因啊?难道是设置冲突?

1、页面前提xaml代码

View Code
 1 <Grid Margin="-6,0,0,0" Background="#CEEDC4" >
2 <DataGrid IsReadOnly="True" Style="{StaticResource dataGridStyle}" CellStyle="{StaticResource dataGridCellStyle}" RowStyle="{StaticResource dataGridRowStyle}" AlternatingRowBackground="#d7e6d9" Height="490" HorizontalAlignment="Center" Name="dataGrid1" VerticalAlignment="Top" Margin="6,31,0,0" Width="780" RowHeaderStyle="{StaticResource dataGridRowHeaderStyle}" ColumnHeaderStyle="{StaticResource dataGridColumnStyle}">
3 <DataGrid.Columns >
4 <DataGridTemplateColumn Header="">
5 <DataGridTemplateColumn.CellTemplate>
6 <DataTemplate>
7 <RadioButton Name="cbCheck" Checked="CheckBox_Checked" ></RadioButton>
8 </DataTemplate>
9 </DataGridTemplateColumn.CellTemplate>
10 </DataGridTemplateColumn>
11
12 <DataGridTextColumn Header="团号" MinWidth="120" Binding="{Binding Path=GroupNum}"/>
13 <!--<DataGridTextColumn Header="旅行社" Width="100" Binding="{Binding Path=TravelagencyName}"/>-->
14 <DataGridTextColumn Header="国籍" MinWidth="70" Binding="{Binding Path=NationalityName}" />
15 <DataGridTextColumn Header="计划/实际人数" MinWidth="100" Binding="{Binding Path=Reserved1}" />
16 <DataGridTextColumn Header="预计入境时间" MinWidth="120" Binding="{Binding Path=Intime}"/>
17 <DataGridTextColumn Header="预计出境时间" MinWidth="120" Binding="{Binding Path=Outtime}"/>
18 <DataGridTextColumn Header="入境航次" MinWidth="70" Binding="{Binding Path=InairlineNum}"/>
19 <DataGridTextColumn Header="出境航次" MinWidth="70" Binding="{Binding Path=Outairlinenum}"/>
20 <DataGridTextColumn Header="状态" Width="*" Binding="{Binding Path=Reserved3}" MinWidth="80"/>
21 </DataGrid.Columns>
22 </DataGrid>

2、app.xaml样式代码

View Code
 1 <!--dataGrid行样式-->
2 <Style x:Key="dataGridRowStyle" TargetType="DataGridRow">
3 <Setter Property="Background" Value="White"/>
4 <Setter Property="Cursor" Value="Hand"></Setter>
5 <Setter Property="VerticalContentAlignment" Value="Center" />
6 <Setter Property="HorizontalContentAlignment" Value="Center"/>
7 <Style.Triggers>
8 <Trigger Property="IsMouseOver" Value="True">
9 <Setter Property="Background" Value="#c4cec5"/>
10 </Trigger>
11 <Trigger Property="IsSelected" Value="True">
12 <Setter Property="Background" Value="#c4cec5"/>
13 <!--<Setter Property="Foreground" Value="Black"/>-->
14 </Trigger>
15 </Style.Triggers>
16 </Style>
17 <!--行标题属性设置-->
18 <Style x:Key="dataGridRowHeaderStyle" TargetType="DataGridRowHeader">
19 <Setter Property="Width" Value="0"/>
20 </Style>
21 <!--列标题属性设置-->
22 <Style x:Key="dataGridColumnStyle" TargetType="DataGridColumnHeader" >
23 <Setter Property="Background" Value="White" />
24 <Setter Property="FontWeight" Value="Bold"/>
25 <Setter Property="FontSize" Value="13" />
26 <Setter Property="BorderBrush" Value="#FF766E6E"></Setter>
27 <Setter Property="BorderThickness" Value="0.4"></Setter>
28 <Setter Property="HorizontalContentAlignment" Value="Center"/>
29 </Style>
30
31 <Style x:Key="dataGridStyle" TargetType="DataGrid">
32 <!--<Setter Property="Margin" Value="5,-110,5,50"></Setter>-->
33 <Setter Property="HorizontalGridLinesBrush" Value="#FF766E6E" />
34 <Setter Property="VerticalGridLinesBrush" Value="#FF766E6E" />
35 <Setter Property="VerticalContentAlignment" Value="Center" />
36 <Setter Property="HorizontalContentAlignment" Value="Center"/>
37 <Setter Property="BorderThickness" Value="2"/>
38 <Setter Property="BorderBrush" Value="#EEEEEE"></Setter>
39 <Setter Property="AutoGenerateColumns" Value="False" />
40 <Setter Property="CanUserAddRows" Value="False" />
41 <Setter Property="CanUserDeleteRows" Value="False" />
42 <Setter Property="CanUserResizeColumns" Value="False" />
43 <Setter Property="CanUserResizeRows" Value="False" />
44 <Setter Property="FontSize" Value="12" />
45 <Setter Property="FontFamily" Value="微软雅黑"></Setter>
46 <Setter Property="SelectionMode" Value="Single" />
47 <Setter Property="SelectionUnit" Value="FullRow" />
48 <Setter Property="Background" Value="White" />
49 <Setter Property="Cursor" Value="Hand"></Setter>
50 </Style>
51
52
53 <Style x:Key="dataGridCellStyle" TargetType="DataGridCell">
54 <Setter Property="VerticalContentAlignment" Value="Center" />
55 <Setter Property="HorizontalContentAlignment" Value="Center"/>
56 <Setter Property="Height" Value="29"/>
57 <Style.Triggers >
58 <Trigger Property="IsSelected" Value="True">
59 <Setter Property="BorderBrush" Value="#a2a2a0"></Setter>
60 <Setter Property="Background" Value="#c4cec5"></Setter>
61 <Setter Property="BorderThickness" Value="0.5"/>
62 <Setter Property="FontSize" Value="12" />
63 <Setter Property="FontFamily" Value="微软雅黑"></Setter>
64 <Setter Property="FontWeight" Value="UltraBlack"></Setter>
65 <Setter Property="Foreground" Value="Black"></Setter>
66 </Trigger>
67 </Style.Triggers>
68
69 </Style>
WPF
roy_coffee的主页 roy_coffee | 初学一级 | 园豆:2
提问于:2012-03-15 21:42
< >
分享
最佳答案
-1

使用模版列把

 <DataGridTemplateColumn Header="团号" Width="12">                     <DataGridTemplateColumn.CellTemplate>                        

<DataTemplate>                            

<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">                                

<TextBlock Text="{Binding Path=GroupNum}"></TextBlock>                            

</StackPanel>                        

 </DataTemplate>                    

</DataGridTemplateColumn.CellTemplate>                

 </DataGridTemplateColumn>

收获园豆:100
v_Bin | 菜鸟二级 |园豆:271 | 2012-03-16 11:35

谢谢,已经解决

roy_coffee | 园豆:2 (初学一级) | 2012-03-21 09:44

请问可以详细说下怎样用模板嘛?菜鸟猜开始学习WPF

々孤星☆泪♂ | 园豆:13 (初学一级) | 2013-09-26 00:34
其他回答(2)
0

假如没有模板列,直接绑定ItemSource的话,如何设置居中

CarsonCui | 园豆:162 (初学一级) | 2013-05-20 11:42
0

Style:

<!--居中DataGridTextColumn-->
<Style x:Key="CenterAlignmentStyle" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center"/>
</Style>

用法:

<DataGridTextColumn IsReadOnly="True" Header="样例" ElementStyle="{StaticResource CenterAlignmentStyle}"  Width="100" />

 

本质,对于DataGridTextColumn来说,就是一个包含了TextBlock的DataGridTemplateColumn,完毕。

颜家大饼 | 园豆:202 (菜鸟二级) | 2016-06-22 20:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册