Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Adding existing controls into a List<>
5 replies. Latest Post by Daze55 on May 27, 2009.
(0)
salosa
Member
3 points
24 Posts
05-26-2009 4:59 AM |
Hi there,
I got this question, would like to know what is the actual code which make this work.In xaml file, I had:
<my:DataGrid x:Name="grdDatePeriods"> <my:DataGrid.Columns> <my:DataGridTemplateColumn x:Name="ColCurrentDay"> <my:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid x:Name="grdCurrentDay" VerticalAlignment="Center" HorizontalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <TextBlock x:Name="lblCurrentDay" Text="{Binding CurrentDay}" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" TextWrapping="NoWrap"></TextBlock> <TextBlock x:Name="lblCurrentDate" Text="{Binding CurrentDate}" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" TextWrapping="NoWrap"></TextBlock> </Grid> </DataTemplate> </my:DataGridTemplateColumn.CellTemplate> </my:DataGridTemplateColumn> <my:DataGridTemplateColumn x:Name="ColDay1"> <my:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid x:Name="grdDays1" VerticalAlignment="Center" HorizontalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <TextBlock x:Name="lblDay1" Text="{Binding Day1}" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" TextWrapping="NoWrap"></TextBlock> <TextBlock x:Name="lblDate1" Text="{Binding Date1}" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" TextWrapping="NoWrap"></TextBlock> </Grid> </DataTemplate> </my:DataGridTemplateColumn.CellTemplate> </my:DataGridTemplateColumn> </my:DataGrid.Columns> </my:DataGrid> -------------------------------------------------------------------------------now, in my c#, I hadList<DataGridTemplateColumn> listDatagridTemplateCol = new List<DataGridTemplateColumn>();listDatagridTemplateCol.Add(ColCurrentDay);listDatagridTemplateCol.Add(ColDay1);
the idea of doing this is for me to easily visible, change the foreground and etc when a condition is met.example: listDatagridTemplateCol[0].Width = new GridLenght(120);Something like this....
but when I checked the list, it is null...it seems like .Add(ColCurrentDay) is not the correct way.anyone got the correct way of doing this code?
Thanks!!
Daze55
227 points
111 Posts
05-26-2009 5:29 AM |
If you set a breakpoint on listDatagridTemplateCol.Add(ColDay1), what is the value of ColDay1 ?
05-26-2009 8:48 PM |
Hi Daze55, it returns null value.
05-27-2009 5:03 AM |
Maybe you are trying to retrieve the columns and to add them to your collection before the initialization of the grid, it's just an hypothesis.What's happening if you retrieve the grids in the template instead of the template itself (for example grdCurrentDay) ?
05-27-2009 8:50 AM |
Hi Daze, I just tried, grdDatePeriods.Columns.Visibility works....I didn't need to pass the ColDay1 to the list and do the visibility function.Thanks for your help! It helps me to think another way round to make it works!
But..this method can work, right?If i have other controls, such as button, which is not inside the Datagrid > DataTemplate.For example: <Button x:name="btnBlock"></Button>I can do as the way I've showed,List<System.Window.Controls.Button> listbutton = new List<System.Window.Controls.Button>();listbutton.Add(btnBlock).This won't return null and will works? Mark me if I'm wrong or right.Thanks!
05-27-2009 12:23 PM |
Well if btnBlock is not null there's no reason it wouldn't be added to your list. A Button is an object as others.