Silverlight Gridcolumn Collapse Visibility in code
I'm having a small problem.
I have this grid with a column:
<data:DataGrid ColumnHeaderStyle="{StaticResource headerStyle}" Foreground="#234BC3" AutoGenerateColumns="False" Name="protocollenBAMDataGrid" LoadingRow="myDataGrid_LoadingRow" SelectionChanged="DataGrid_SelectionChanged">
<data:DataGrid.Columns>
<data:DataGridTemplateColumn Header="Resend" x:Name="ResendColumn">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Resend" Tag="{Binding MsgID}" Foreground="#234BC3" Click="dataGridHL7_Click"></Button>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn> ...
If i set the visibility of the "ResendColumn" to collapsed, it works fine.
However I need to do this in code, so I tried doing this in the loading event:
ResendColumn.Visibility = Visibility.Collapsed;
And i开发者_如何转开发t gives me a nullref exception:
Object reference not set to an instance of an object.
I don't understand why?
I get the same behaviour in SL 4. I guess it is related to the column not being a UIElement and thus instantiated by the DataGrid itself (not by the usual stuff happening in InitializeComponent).
I had to access the columns through the Columns property of the DataGrid. And there, setting the visibility works.
精彩评论