WPF radio button with Image

后端 未结 2 1171
一个人的身影
一个人的身影 2020-12-07 02:29

I have to create something similar to the picture. If one of the button is clicked the others should become darker. Thanks a lot!

That\'s what I need

相关标签:
2条回答
  • 2020-12-07 02:53

    after some time I found another approach. Instead of custom RadioButton, one can use ListBox with custom ItemTemplate

    ViewModel for a single 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">
    
        <!--changing default ListBoxItem to hide selection highlight-->
        <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>
    
        <!--changing default orientation-->
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type wpf2:CountryVm}">
                <!--circle image border-->
                <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"/>
    
                    <!--changing selected item opacity via trigger-->
                    <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

    0 讨论(0)
  • 2020-12-07 02:54

    you can change Opacity when RadioButton is not checked via style trigger

    <RadioButton.Style>                    
        <Style TargetType="RadioButton">                        
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="False">
                    <Setter Property="Opacity" Value="0.5"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </RadioButton.Style>
    

    image inside can be created by modification of Template

    <RadioButton.Template>
        <ControlTemplate TargetType="RadioButton">
            <!-- new template -->
        </ControlTemplate>
    </RadioButton.Template>
    

    default template can be found here


    my primitive template looks like this (i have added 3 radioButtons into ItemsControl, the 2nd is checked)

    <StackPanel Grid.Row="0" Grid.Column="1">
        <StackPanel.Resources>
            <Style x:Key="Flag" TargetType="RadioButton">
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="False">
                        <Setter Property="Opacity" Value="0.5"/>
                    </Trigger>
                </Style.Triggers>
    
                <Setter Property="BorderThickness" Value="2"/>
    
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="RadioButton">
                            <Border BorderThickness="{TemplateBinding BorderThickness}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    Background="Transparent"
                                    CornerRadius="20">                                    
                                <Image Source="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Resources>
    
        <ItemsControl>
            <RadioButton Content="../Resources/radio.png" Style="{StaticResource Flag}" BorderBrush="Red" Width="40" Height="40"/>
            <RadioButton Content="../Resources/radio.png" Style="{StaticResource Flag}" BorderBrush="Orange" Width="40" Height="40"/>
            <RadioButton Content="../Resources/radio.png" Style="{StaticResource Flag}" BorderBrush="Green" Width="40" Height="40"/>
        </ItemsControl>
    </StackPanel>
    
    0 讨论(0)
提交回复
热议问题