0

I'm using Silverlight 5.1.30514.0 with Windows 8, and I have a case, in which depending of a field in a table I need to present different amounts of CheckBox, if the field is 4, then 4 CheckBox, if the field is 6, then 6 CheckBox, and this field can be 4, 6, 10 or 12, then I make at Resources:

<DataTemplate x:Name="chkField4" DataType="ContentControl">
  <Grid Name="grd">
    <Border BorderBrush="Black" BorderThickness="1" Margin="0,0,0,0" Background="DarkSeaGreen" />
        <CheckBox Content="2" Name="chk2" Grid.Column="1" Margin="20,20,20,20"  Checked="CheckBox_Checked" />
        <CheckBox Content="1" Name="chk1" Grid.Column="1" Margin="20,60,20,20"  Checked="CheckBox_Checked"/>
        <CheckBox Content="4" Name="chk4" Grid.Column="1" Margin="80,20,20,20"  Checked="CheckBox_Checked"/>
        <CheckBox Content="3" Name="chk3" Grid.Column="1" Margin="80,60,20,20"  Checked="CheckBox_Checked"/>
   </Grid>
</DataTemplate>

And another:

<DataTemplate x:Name="chkField6" DataType="ContentControl">
  <Grid Name="grd">
    <Border BorderBrush="Black" BorderThickness="1" Margin="0,0,0,0" Background="DarkSeaGreen" />
        <CheckBox Content="2" Name="chk2" Grid.Column="1" Margin="20,20,20,20"  Checked="CheckBox_Checked" />
        <CheckBox Content="1" Name="chk1" Grid.Column="1" Margin="20,60,20,20"  Checked="CheckBox_Checked"/>
        <CheckBox Content="4" Name="chk4" Grid.Column="1" Margin="80,20,20,20"  Checked="CheckBox_Checked"/>
        <CheckBox Content="3" Name="chk3" Grid.Column="1" Margin="80,60,20,20"  Checked="CheckBox_Checked"/>
        <CheckBox Content="6" Name="chk6" Grid.Column="1" Margin="140,20,20,20"  Checked="CheckBox_Checked"/>
        <CheckBox Content="5" Name="chk5" Grid.Column="1" Margin="140,60,20,20"  Checked="CheckBox_Checked"/>
   </Grid>
</DataTemplate>

And 2 more similar (chkField10, chkField12) for each case. And I put a ContentControl:

<ContentControl Name="chk" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>

And at behind code depending of a field, if field=4:

chk.ContentTemplate = chkField4;

Now, I want to Access to each ComboBox in order to review if is checked, something like:

if (chk2.IsChecked) ...
if (chk1.IsChecked) ...

But, the problem is: chk1, chk2, ... are not available to ask for them, and I would like to make a generic cycle with a "for" or "foreach" 1 to n, where n is the amount of CheckBox in the ContentControl in a time

I don't know how I can do this, or if I have done is right, someone could guide me?

I'm realy new in this environment, thanks

Cyndy
  • 93
  • 1
  • 10
  • Hey Cyndy, are you even doing a quick search for this sort of thing whether it be on [here](http://stackoverflow.com/questions/26258512/get-the-controls-inside-datatemplate-control) or [elsewhere](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=controls+inside+datatemplate+silverlight) before you ask? You'd probably find your way to something regarding [VisualTreeHelper](http://blog.falafel.com/finding-controls-by-type-in-silverlight-and-wpf/) pretty quick if you had. – Chris W. Oct 09 '14 at 17:45
  • Yes Chris W., definitely, I tried several options of here and MSDN forums, but apparently not located within the ContenControl children, takes it as a single control no matter what is inside the DataTemplate defined – Cyndy Oct 09 '14 at 18:32

1 Answers1

0

You can use an extension method that AnthonyWJones created in this post

Here is the code

public static class VisualTreeEnumeration
{
   public static IEnumerable<DependencyObject> Descendents(this DependencyObject root)
   {
     int count = VisualTreeHelper.GetChildrenCount(root);
     for (int i=0; i < count; i++)
     {
       var child = VisualTreeHelper.GetChild(root, i);
       yield return child;
       foreach (var descendent in Descendents(child))
         yield return descendent;
     }
   }
}

Then, to access your checkboxes within the contentcontrol, you can do it like this, assuming your contentcontrol's name is chk. (Try to use a better name for contentcontrol to avoid confusion)

var checkBoxes = chk.Descendents().OfType<CheckBox>();

Then you can do a foreach on the collection of checkboxes.

foreach(var checkBox in checkBoxes)
{
    if(checkBox.IsChecked)
    {
        //Do Something
    }
}

Hope that helps.

Community
  • 1
  • 1
Krishna Veeramachaneni
  • 2,131
  • 3
  • 18
  • 24
  • Please!!! Krishna, show me an example of a foreach with the checkboxes collection, I don't know how make this, your suggestion is work well – Cyndy Oct 09 '14 at 18:04