WPF GridView中的CellTemplate失效的原因

最近做一个ListView的Style时,发现一个问题, 就是我的GridView的 GridViewColumn 的CellTemplate无论是用StaticResource还是DynamicResource,都是没有效果。

原因: GridViewColumn 用了 DisplayMemberBinding  使得CellTemplate失效。 

解决办法: 去掉DisplayMemberBinding, 在CellTemplate的模板中指定Path路径。 比如:

                                <DataTemplate x:Key="GridViewHeaderTemplate1">
                                                                 < StackPanel >
                                                                                 < TextBlock   Text ="{ Binding Mode =OneWay}"/>
                                                                                 < TextBox   TextWrapping ="Wrap" Text ="" MinWidth ="25"/>
                                                                 </ StackPanel >
                                                 </ DataTemplate >
                                                
                                                 < DataTemplate x : Key ="StatusDataTemplate">
                                                                 < CheckBox IsChecked ="{ Binding Status }"></ CheckBox >
                                                 </ DataTemplate >

< GridViewColumn HeaderTemplate ="{ StaticResource GridViewHeaderTemplate1 }" CellTemplate ="{ StaticResource StatusDataTemplate }"   Header ="Status" />

如果你不想用CellTemplate指定Binding的path, 可以通过 < GridViewColumn  HeaderTemplate ="{ StaticResource  GridViewHeaderTemplate1 }"   DisplayMemberBinding ="{ Binding  Status }"    Header ="Status" />

你可能感兴趣的:(WPF)