您的位置:首页 > 运维架构

How To Binding the DataContext Property To DataGrid

2012-02-20 10:40 405 查看
When we use the NVVM design mode in wpf develop. We also binding some property to datagrid as well. For example we can define datagrid to binding a list in such place.

<DataGrid  x:Uid="dgByKeys"
ItemsSource="{Binding Keys}"
Style="{StaticResource DgNormalSty}"
ScrollBar.Scroll="ExportByKeys_Scroll"
ScrollViewer.ScrollChanged="ExportByKeys_ScrollChanged"
Sorting="ExportByKeys_Sorting"
Height="230"
HorizontalAlignment="Stretch"


But when we will want to binding a propoerty not include the list keys(it is not a key property),how we can binding it in datagrid element?

It is a solution:

<DataGrid.Columns>
<DataGridTemplateColumn >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate >
<CheckBox x:Uid="chkAllIsChecked"
IsThreeState="False"
IsChecked="{Binding IsAllChecked}"
DataContext="{Binding ElementName=tabGrid, Path=DataContext}"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<CheckBox x:Uid="chkKeyIsChecked"
IsThreeState="False"
IsChecked="{Binding IsSelected,Mode=TwoWay, NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" Margin="4,0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


The property IsAllChecked is not key's property,it is context, so we define

DataContext="{Binding ElementName=tabGrid, Path=DataContext}"/>


And IsSelected is key's property,so we also define:

<CheckBox x:Uid="chkKeyIsChecked"
IsThreeState="False"
IsChecked="{Binding IsSelected,Mode=TwoWay, NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" Margin="4,0"/>


There is work perfect.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐