博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WPF 4 中DataGrid的模板列做双向数据绑定

Posted on 2011-05-26 11:28  linFen  阅读(4952)  评论(3编辑  收藏  举报
  

WPF 4 中DataGrid的模板列做双向数据绑定时,需要在绑定中指定UpdateSourceTrigger,如下:(WPFToolkit February 2010)

<DataGrid VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" 
Name=
"dataGrid1" AutoGenerateColumns="False" ItemsSource="{Binding}" HorizontalGridLinesBrush=
"{DynamicResource GridLinesBrush}" VerticalGridLinesBrush="{DynamicResource GridLinesBrush}"
 AlternatingRowBackground="{DynamicResource RowBackgroundBrush}" CanUserAddRows="False" 
CanUserResizeRows=
"False" CanUserDeleteRows="False" IsEnabled="True" Margin="0" RowHeight="20" 
 CellStyle=
"{DynamicResource DataGridCellStyle2}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}" 
RowStyle=
"{DynamicResource DataGridRowStyle1}" RowHeaderStyle="{DynamicResource DataGridRowHeaderStyle1}" 
ItemsPanel=
"{DynamicResource ItemsPanelTemplate1}" RowHeaderWidth="0" IsSynchronizedWithCurrentItem="True"
 SelectionMode="Single" MinWidth="10" BorderThickness="0" GridLinesVisibility="Horizontal" ColumnHeaderHeight="22"
 ScrollViewer.CanContentScroll="False" >
  <DataGrid.Columns>
    <DataGridTemplateColumn Header="选择" CanUserReorder="False">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Cursor="Hand"/>
        </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTextColumn Header="标题" Binding="{Binding Title, Mode=OneWay}" />
  </DataGrid.Columns>
</DataGrid>

  IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

  WPFToolkit February 2010则不需要。