WPF中在使用DateGrid时,根据绑定数据源来显示与隐藏整列


XAML代码:

 1           <FrameworkElement x:Name="dummyElement" Visibility="Collapsed"/>
 2                 <DataGrid Grid.Column="0" x:Name="DgData" SelectionMode="Extended" Margin="5" BorderBrush="#BBB" BorderThickness="1"
 3                   Style="{StaticResource DefaultDataGrid}"
 4                   ItemsSource="{Binding OcPlanFlowSourceCanEdit }"
 5                   SelectedItem="{Binding SelectedEntityMaterial, Mode=TwoWay}" >
 6                     <DataGrid.Columns>
 7                         ...
 8                         ...
 9                         <DataGridTextColumn Header="投料数量" Width="100" Binding="{Binding Path=Qty,StringFormat=0.00}" ElementStyle="{StaticResource contentRightStyle}" Visibility="{Binding DataContext.RedCanNotChange, Source={x:Reference dummyElement},Converter={StaticResource BoolToVisibleConverter}}"/>
10                         ...
11                         ...
12                     DataGrid.Columns>
13                 DataGrid>
BoolToVisibleConverter转换器见其它文章