Is there a way to group RadioButtons created from the ItemTemplate of an ItemsControl

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

Above the code is the ItemTemplate of my ItemsControl, I want all the created Radiobuttons instances to behave as if they were in a group, I know the reason, because the created RadioButtons are not adjacent in the visualtree.

Any solution or workaround for grouping together ?. The GroupName property here also has no effect.

[Refresh] I'm trying to do this in Silverlight

+4
source share
2 answers

The problem is that the behavior of RadioButton.GroupName depends on the logical tree in order to find a common ancestor and use it effectively for this part of the tree, but silverlight ItemsControl does not support the logical tree. This means that in your example, the RadioButton Parent property is always null

I installed 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/

+4
source

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

Here is an example 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"> <Grid> <Grid.Resources> <XmlDataProvider x:Key="flickrdata" Source="http://api.flickr.com/services/feeds/photos_public.gne?tags=flower&amp;lang=en-us&amp;format=rss_200"> <XmlDataProvider.XmlNamespaceManager> <XmlNamespaceMappingCollection> <XmlNamespaceMapping Prefix="media" Uri="http://search.yahoo.com/mrss/"/> </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> 

PS: For grouping into work items, the radio buttons must have the same parent item (as a rule, they are generated using ItemsControl)

+3
source

Source: https://habr.com/ru/post/1277466/


All Articles