wpf DataGrid 里的列模版的值绑定
第一步:建立一个模版文件,如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Main.TemplateResource"> <DataTemplate x:Key="fileTypeIcon"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Stretch"> <!--<TextBlock Cursor="Hand" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,0" Name="btnMin" Text="" Style="{StaticResource FontAwesome}" FontSize="14" Foreground="#f8c92a"></TextBlock>--> <TextBlock Text="{Binding RowData.Row.FileName}"></TextBlock> </StackPanel> </DataTemplate> </ResourceDictionary>
第二步:引用模版文件 (由于我是用在用户控件里,所以没有使用Window.Resources 这里用的是 Control.Resources)
<Control.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Main;component/TemplateResource/FileTypeIconResource.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Control.Resources>
第三步:使用模版
CellTemplate="{DynamicResource fileTypeIcon}"
<dxg:GridControl Name="grid" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionMode="Row"> <dxg:GridControl.View> <dxg:TableView NavigationStyle="Row" x:Name="gridView" AllowScrollAnimation="True" ShowGroupPanel="False" /> </dxg:GridControl.View> <dxg:GridControl.Columns> <dxg:GridColumn Header="" CellTemplate="{DynamicResource fileTypeIcon}" FieldName="Type" HorizontalHeaderContentAlignment="Center" Width="35" ></dxg:GridColumn> <dxg:GridColumn Header="文件名" HorizontalHeaderContentAlignment="Left" FixedWidth="True" FieldName="FileName" Width="*" /> <dxg:GridColumn Header="修改时间" HorizontalHeaderContentAlignment="Center" FieldName="EditDate" Width="100" /> <dxg:GridColumn Header="大小" HorizontalHeaderContentAlignment="Center" FieldName="FileSize" Width="80" /> </dxg:GridControl.Columns> </dxg:GridControl>
备注:
<TextBlock Text="{Binding RowData.Row.FileName}"></TextBlock>
这里的RowData.Row.FileName的使用方法害我浪费了好长时间。