I am a newbie with DataGrids in WPF C#.
I have two DataGrids on two different tab controls.
Now one of them works (which means I am able to dynamically create and add columns and access datagrid1 and its methods)
While with other one datagrid2 I am not able to do the above in any private methods. (I copy pasted datagrid1 properties and renamed it datagrid2)
However with datagrid2, when I use inside the Windows_Loaded method, I am able to do these things.
What am I doing wrong?
Here’s the code:
<my3:DataGrid AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False" Block.TextAlignment="Center" HeadersVisibility="Column"
AlternationCount="2" ItemsSource="{Binding}" IsReadOnly="True"
AlternatingRowBackground="LightYellow" Margin="6,57.772,12.489,6" Name="datagrid2"
xmlns:my3="http://schemas.microsoft.com/wpf/2008/toolkit">
</my3:DataGrid>
Generally speaking, never try to access and manipulate
UIElements in the constructor.Instead, handle the
Loadedevent of your container (usually aUserControl,Control,Window, orPage) and manipulate yourUIElements there.