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.
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:
The property IsAllChecked is not key's property,it is context, so we define
And IsSelected is key's property,so we also define:
There is work perfect.
<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.
相关文章推荐
- How to populate the datagrid on background thread with data binding by using Visual C#
- [WPF] How to bind to data when the DataContext is not inherited
- How to bind to data when the DataContext is not inherited【项目】
- How to get the rowid when insert the data to Oracle database
- How to use Events in the Context of C#
- Consuming the Object Data Source--Binding the Object Data Source to the GridView
- How to bind a GridView to a list of multiple types? NHibernate proxy causing problems with databinding [From stack overflow]
- How to get the Diagnostic data and debug file of Sales Order pick release
- Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
- How to print property if declaring in the format of @synthesize a=_a;
- *****How to scroll in the grid when editing data in a cell
- [Yii Framework] how to add additional data to the user session
- How To Perform a Full Export And Exclude Certain Schemas Using The Data Pump API? [ID 1340781.1]
- FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- flex/in the datagrid,how to get the new value of cell in the datagrid after edit
- Adding a Data-Dependent DropDownList to the DataGrid's Footer
- How to use relative paths without including the context root name?
- How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- Windows SDK Registry: How can I write data to the registry?
- How to select the data type in SQLserver database such as varchar, nvarchar