Skip to main content

Microsoft Silverlight

Answered Question Adding existing controls into a List<>RSS Feed

(0)

salosa
salosa

Member

Member

3 points

24 Posts

Adding existing controls into a List<>

 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 had
List<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
Daze55

Member

Member

227 points

111 Posts

Re: Adding existing controls into a List<>

If you set a breakpoint on listDatagridTemplateCol.Add(ColDay1), what is the value of ColDay1 ?

salosa
salosa

Member

Member

3 points

24 Posts

Re: Adding existing controls into a List<>

 Hi Daze55, it returns null value.

Daze55
Daze55

Member

Member

227 points

111 Posts

Re: Adding existing controls into a List<>

 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) ?

salosa
salosa

Member

Member

3 points

24 Posts

Re: Adding existing controls into a List<>

Hi Daze, I just tried, grdDatePeriods.ColumnsIdea.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!

Daze55
Daze55

Member

Member

227 points

111 Posts

Answered Question

Re: Adding existing controls into a List<>

 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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities