views:

255

answers:

2

Hi everyone

I have a propably simple problem, that i just can't seem to figure out:

I've made an ItemsControl which has its datacontext set and shows the data as pairs of Checkboxes and TextBlocs:

<ItemsControl Name="listTaskTypes" Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" ItemsSource="{Binding}" Margin="10,0,0,0" VerticalAlignment="Top" Loaded="listTaskTypes_Loaded">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Name="checkBoxTypeId" Tag="{Binding Path=TaskTypeID}"/>
                <TextBlock FontSize="11pt" FontFamily="Helvetica" Text="{Binding Path=Text}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

My problem is that in the Loaded event of the ItemsControl, the checkboxes does not exist yet. How can i get an event when the ItemsControl is completely loaded or is this not possible?

A: 

listTaskTypes.ItemContainerGenerator.StatusChanged event handler can give you the notification on each item created on the ItemsControl.

Yeah Loaded is just the ItemsControl loaded event, the items might not have created at that moment. Just curious as to what you are trying to achieve here?. I guess you are trying to get the instance of CheckBox in the code behind? There may be better way using binding to achieve what you are looking for.

Jobi Joy
You are indeed right - I've made a method that retrieves an instance of all the checkboxes, which i then set to selected or not depending on some conditions in the database. These conditions are not simple true/false fields, so i do not know how todo through binding.The solution for me was to just set them via their own loaded event instead and then first retrieve them all when the user hits the Save button at which point they of course exist.
Kaare
I believe the logic do to find out the Checkbox status can really be in the ViewModel side and a bool property can be exposed to bind the checkbox. So I really recommend to use MVVM here and avoid the codebehind
Jobi Joy
A: 

Try the DataContextChanged event!

When the DataContext changes, the control should be Loaded, and you can be sure it has a DataContext set as well.

Hope this helps

Arcturus