views:

1484

answers:

2
 <DataTemplate x:Key="Genre_DataTemplate">
        <RadioButton GroupName="One" Content="{Binding...
 </DataTemplate>

Above code is the ItemTemplate of my ItemsControl, I want all the Radiobuttons instantiated should behave as if it is in a group, I know the reason because the generated RadioButtons are not adjacent in the visualtree.

Any solution or workaround to group them together?. GroupName property also doesn't have any effect here.

[Update] I am trying this in Silverlight

+2  A: 

I think the problem is somewhere else in the control tree. Can you post more details?

Here is a sample xaml code that works as expected:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
 <Grid>
    <Grid.Resources>
       <XmlDataProvider x:Key="flickrdata" Source="http://api.flickr.com/services/feeds/photos_public.gne?tags=flower&amp;amp;lang=en-us&amp;amp;format=rss_200"&gt;
          <XmlDataProvider.XmlNamespaceManager>
             <XmlNamespaceMappingCollection>
                <XmlNamespaceMapping Prefix="media" Uri="http://search.yahoo.com/mrss/"/&gt;
             </XmlNamespaceMappingCollection>
          </XmlDataProvider.XmlNamespaceManager>
       </XmlDataProvider>
       <DataTemplate x:Key="itemTemplate">
        <RadioButton GroupName="One">
          <Image Width="75" Height="75" Source="{Binding Mode=OneWay, XPath=media:thumbnail/@url}"/>
        </RadioButton>
       </DataTemplate>
       <ControlTemplate x:Key="controlTemplate" TargetType="{x:Type ItemsControl}">
          <WrapPanel IsItemsHost="True" Orientation="Horizontal"/>
       </ControlTemplate>
    </Grid.Resources>
    <ItemsControl
       Width="375"
       ItemsSource="{Binding Mode=Default, Source={StaticResource flickrdata}, XPath=/rss/channel/item}"
       ItemTemplate="{StaticResource itemTemplate}"
       Template="{StaticResource controlTemplate}">
    </ItemsControl>
 </Grid>

</Page>

P.S.: In order grouping to work elements radio buttons should have same parent (as they usually have when generated from ItemsControl)

ligaz
This is proper solution for this issue in WPF, making the controltemplate of ItemsControl as Panel with ItemsHost, But we dont have ItemHost in Silverlight (I am not sure). Any workaround in Silverlight
Jobi Joy
+3  A: 

Hi Joby

The problem is that the RadioButton.GroupName behavior depends on the logical tree to find a common ancestor and effectively scope it's use to that part of the tree, but silverlight's ItemsControl doesn't maintain the logical tree. This means, in your example, the RadioButton's Parent property is always null

I built a simple attached behavior to fix this. It is available here: http://www.dragonshed.org/blog/2009/03/08/radiobuttons-in-a-datatemplate-in-silverlight/

Karim Hernandez