locked
Receiving Input From Controls in a ListView's DataTemplate

    Question

  • I have a ListView with a DataTemplate that contains several RadioButtons. The RadioButtons are, of course, there because I want the user to give input for each item. However, I am having trouble figuring out how to access the RadioButtons from my code to obtain that input. How can I access the RadioButtons that are in the DataTemplate? Thanks.

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

    Thursday, June 05, 2014 8:41 PM

Answers

  • I think I finally found a way to do what I want. When I use the following code:
    Dim curritemindex As Integer = 0
    Dim tempcontainer As ListViewItem = CType(Me.lvwResults.ContainerFromIndex(curritemindex), ListViewItem)
    Dim root As StackPanel = CType(tempcontainer.ContentTemplateRoot, StackPanel)
    I can view everything I need by looking at the properties of root just like I would look at anything else. I don't know if this is necessarily the best way to do this (or maybe it is), but it accomplishes my goal for now. Thank you for your help.

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

    Friday, June 06, 2014 2:32 AM

All replies

  • Data bind the button's IsChecked property to your data source.

    If you want to fiddle with the button directly you can get a reference to it in event handlers, when it is created in GetContainerForItemOverride, or by navigating the visual tree with a VisualTreeHelper.

    In most cases data binding is the best cleanest way to do this. Of the rest, the event handler will most likely be best.

    --Rob

    Thursday, June 05, 2014 9:19 PM
    Owner
  • Binding to the IsChecked property would not work for me, because the data source is not what I need to use it for. I would prefer to use the VisualTreeHelper so that I can look at the RadioButtons directly, but I am having trouble understanding the example at http://msdn.microsoft.com/en-us/library/bb613579.aspx My exact code for the ListView and DataTemplate are:
    <ListView x:Name="lvwResults" FontFamily="Times New Roman" FontSize="24" Foreground="Black" HorizontalContentAlignment="Left" Margin="5,0,0,0" SelectionMode="None">
    	<ListView.ItemTemplate>
    		<DataTemplate>
    			<StackPanel Margin="5,0" Orientation="Horizontal">
    				<TextBlock Style="{StaticResource InputTextStyle}" Text="{Binding Name}" Margin="0,0,5,0" Width="{Binding Width}" VerticalAlignment="Center"/>
    				<RadioButton Style="{StaticResource ResultsRadioButtonStyle}" GroupName="{Binding Name}" Content="Yes" Tag="{Binding Index}"/>
    				<RadioButton Style="{StaticResource ResultsRadioButtonStyle}" GroupName="{Binding Name}" Content="No" Tag="{Binding Index}" IsChecked="True"/>
    			</StackPanel>
    		</DataTemplate>
    	</ListView.ItemTemplate>
    </ListView>

    I will also note that when I bind the ItemsSource, I use an anonymous type, which is another reason I cannot use the binding to IsChecked method. How would I use the VisualTreeHelper to view the RadioButtons? Thanks.

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

    Friday, June 06, 2014 2:05 AM
  • I think I finally found a way to do what I want. When I use the following code:
    Dim curritemindex As Integer = 0
    Dim tempcontainer As ListViewItem = CType(Me.lvwResults.ContainerFromIndex(curritemindex), ListViewItem)
    Dim root As StackPanel = CType(tempcontainer.ContentTemplateRoot, StackPanel)
    I can view everything I need by looking at the properties of root just like I would look at anything else. I don't know if this is necessarily the best way to do this (or maybe it is), but it accomplishes my goal for now. Thank you for your help.

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

    Friday, June 06, 2014 2:32 AM