locked
Rebinding ItemsControl in DataTemplate

    Question

  • I have the following Pivot control in my XAML:
    <Pivot x:Name="pivRecipients" Grid.Row="1" Background="Transparent">
    	<Pivot.ItemContainerStyle><Style TargetType="PivotItem"><Setter Property="Margin" Value="0"/></Style></Pivot.ItemContainerStyle>
    	<Pivot.HeaderTemplate><DataTemplate><TextBlock Text="{Binding Name}" FontSize="60" FontFamily="Arial"/></DataTemplate></Pivot.HeaderTemplate>
    	<Pivot.ItemTemplate>
    		<DataTemplate>
    			<ScrollViewer>
    				<ItemsControl ItemsSource="{Binding Gifts,Converter={StaticResource GiftsWithIndex}}">
    					<ItemsControl.ItemsPanel><ItemsPanelTemplate><StackPanel/></ItemsPanelTemplate></ItemsControl.ItemsPanel>
    					<ItemsControl.ItemTemplate><DataTemplate><RadioButton Style="{StaticResource GiftRadioButtonStyle}" Content="{Binding}" Checked="Gift_Checked" Tag="{Binding Index}" Loaded="Gift_Loaded"/></DataTemplate></ItemsControl.ItemTemplate>
    				</ItemsControl>
    			</ScrollViewer>
    		</DataTemplate>
    	</Pivot.ItemTemplate>
    </Pivot>
    You will notice that the Pivot's ItemTemplate contains an ItemsControl. When the Pivot's SelectionChanged event occurs, I want to rebind the ItemsControl for that specific ItemsControl (so that the Loaded event of each item is triggered). I could, of course, just rebind the whole Pivot, but that seems somewhat inefficient. Can anybody tell me how to rebind the ItemsControl for only the current selection? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, January 16, 2015 4:40 PM

All replies

  • I'm assuming you are creating a WP81 Store App.

    Why don't you simply call the Gift_Loaded in code when the SelectionChanged is fired?

    You can do a simple check: if the PivotItem was already loaded then call Gift_Loaded in code, otherwise the UI will fire the event Gift_Loaded.

    • Edited by dsmwb Friday, January 16, 2015 8:06 PM
    Friday, January 16, 2015 5:32 PM
  • How would I access the list of Gifts for to do that? I would need to call Gift_Loaded for each gift, and I believe that would be more complicated, but I would be willing to do that if it is the only way. However, while searching for an answer, I found the UpdateSource() method and the UpdateSourceTrigger property of the binding. I have never used either of these, but they sounded like they might be able to help if I could figure out how to use them (but I would probably need help figuring that out). Any ideas? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Saturday, January 17, 2015 12:12 AM
  • The main question is: why do you need to call again the Loaded event? I think you are trying to update items, but I would suggest to read the documentation of UpdateSourceTrigger for bindings.

    However, the SelectionChangedEventArgs of the SelectionChanged contains information about the PivotItem that is going to be shown. You can use VisualTreeHelper.GetChildrenCount(PivotItem) to get the number of items in it and VisualTreeHelper.GetChild(PivotItem, Index) to get the sub item at Index.


    • Edited by dsmwb Saturday, January 17, 2015 12:25 AM
    Saturday, January 17, 2015 12:25 AM
  • My reason for calling the Loaded event is to set the IsChecked property of the RadioButton calling the Loaded event. You will notice that in my original post, the RadioButton in the DataTemplate has the Loaded event set to Gift_Loaded. This is how I initially select the desired RadioButton. Here is the code for Gift_Checked and Gift_Loaded:
    Private Sub Gift_Checked(sender As Object, e As RoutedEventArgs)
    	Me.SelectedGiftIndex = CInt(CType(sender, RadioButton).Tag)
    End Sub
    
    Private Sub Gift_Loaded(sender As Object, e As RoutedEventArgs)
    	CType(sender, RadioButton).IsChecked = Me.SelectedGiftIndex = CInt(CType(sender, RadioButton).Content.GetType().GetRuntimeProperty("Index").GetValue(CType(sender, RadioButton).Content))
    End Sub
    You will notice that Gift_Loaded uses the sender parameter, so I would need access to the RadioButton(s) if calling Gift_Loaded from code. I don't think VisualTreeHelper would help me, because if I understand correctly, VisualTreeHelper would be giving me the elements from the ControlTemplate, not the RadioButton.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Saturday, January 17, 2015 1:28 AM
  • Why don't you bind the IsChecked property?
    • Edited by dsmwb Saturday, January 17, 2015 1:48 AM
    Saturday, January 17, 2015 1:45 AM
  • At first, that is what I tried to do. However, I was having trouble with that. It was causing problems, first of all, I did not have anything to bind it to, so I tried creating a hidden element, but there were still multiple problems. After a week of trying to get that to work, I managed to find something that worked (using the Loaded event). But then I realized that depending on the scenario (which ItemsControl was getting bound when SelectedGiftIndex had different values), that did not update the ItemsControls. Also, wouldn't Binding IsChecked cause the whole Pivot to be rebound (which is what I am trying to avoid)?

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Saturday, January 17, 2015 3:04 AM
  • Actually, if you bind the IsChecked property you also need to use the UpdateSourceTrigger to keep the binding updated. In my experience, a binding on an item does not cause the parent to be refreshed.

    Saturday, January 17, 2015 3:13 AM
  • OK, maybe I did something wrong when I tried binding IsChecked before. However, like I said, I have never used UpdateSourceTrigger. Can anybody help me figure out how to use it here?

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Saturday, January 17, 2015 11:57 PM
  • I haven't found examples for WP81 in VB, but you can read this old project:

    https://code.msdn.microsoft.com/windowsapps/Data-Binding-Demo-82a17c83



    • Edited by dsmwb Sunday, January 18, 2015 3:40 AM
    Sunday, January 18, 2015 3:39 AM
  • OK, now I remember why I couldn't use Binding to set IsChecked, it is because I wouldn't be able to bind the ConverterParameter (because it is not a DependencyProperty). I need to be able to bind ConverterParameter because both the value and parameter for my IValueConverter come from bindings. The pages I found on workarounds for this did not seem to help. Any other ideas? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, January 18, 2015 8:30 PM
  • Please post a sample project, otherwise it is really difficult to image what you are trying to achieve.
    Sunday, January 18, 2015 9:32 PM