Style设置时,无法绑定到数据,需要这样写
1 <DataGridTextColumn Header="呵呵" Binding="{Binding ID}" > 2 <DataGridTextColumn.ElementStyle> 3 <Style TargetType="TextBlock"> 4 <Setter Property="Visibility" Value="{Binding ElementName=WW,Path=DataContext.Visi}" /> 5 </Style> 6 </DataGridTextColumn.ElementStyle> 7 <DataGridTextColumn.HeaderStyle> 8 <Style TargetType="DataGridColumnHeader"> 9 <Setter Property="Visibility" Value="{Binding ElementName=WW,Path=DataContext.Visi}" /> 10 </Style> 11 </DataGridTextColumn.HeaderStyle> 12 <DataGridTextColumn.CellStyle> 13 <Style TargetType="DataGridCell"> 14 <Setter Property="Visibility" Value="{Binding ElementName=WW,Path=DataContext.Visi}" /> 15 </Style> 16 </DataGridTextColumn.CellStyle> 17 </DataGridTextColumn>
另外有其他实现的例子:https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/