After a while I found a different approach. Instead of a custom RadioButton, you can use a
ListBox with a custom
ItemTemplateViewModel for one item
public class CountryVm { public CountryVm() { ImageUri = new Uri("Resources/rgb.png", UriKind.Relative); } public string Name { get; set; } public Uri ImageUri { get; set; } }
ListBox Markup
<ListBox Name="Countries" ItemsSource="{Binding}" SelectionMode="Single" HorizontalAlignment="Center" VerticalAlignment="Top" BorderThickness="0"> <ListBox.Resources> <Style TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border Background="Transparent" SnapsToDevicePixels="true"> <ContentPresenter /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.Resources> <ListBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ListBox.ItemsPanel>
<ListBox.ItemTemplate> <DataTemplate DataType="{x:Type wpf2:CountryVm}"> <Border Name="Border" BorderThickness="1" BorderBrush="Black" Background="{x:Null}" Width="48" Height="48" CornerRadius="24" Margin="4" ToolTip="{Binding Name}"> <Image Source="{Binding Path=ImageUri}" Stretch="None"/> <Border.Style> <Style TargetType="Border"> <Setter Property="Opacity" Value="0.5"/> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Value="True"> <Setter Property="Opacity" Value="1"/> </DataTrigger> </Style.Triggers> </Style> </Border.Style> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
test DataContext :
DataContext = new List<CountryVm> { new CountryVm {Name = "123"}, new CountryVm {Name = "Abc"}, new CountryVm {Name = "Xyz"}, };
result

source share