C# 带图像的WPF单选按钮

C# 带图像的WPF单选按钮,c#,wpf,xaml,wpf-controls,C#,Wpf,Xaml,Wpf Controls,我必须创造一些类似于图片的东西。如果单击其中一个按钮,其他按钮应变暗。非常感谢 这就是我需要的 如果未通过样式触发器选中单选按钮,则可以更改不透明度 <RadioButton.Style> <Style TargetType="RadioButton"> <Style.Triggers> <Trigger Propert

我必须创造一些类似于图片的东西。如果单击其中一个按钮,其他按钮应变暗。非常感谢

这就是我需要的


如果未通过样式触发器选中
单选按钮
,则可以更改
不透明度

<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>
可以找到默认模板


我的基本模板如下所示(我在
ItemsControl
中添加了3个单选按钮,选中了第2个)


过了一段时间,我找到了另一种方法。您可以将
列表框
与自定义
项目模板

<RadioButton.Template>
    <ControlTemplate TargetType="RadioButton">
        <!-- new template -->
    </ControlTemplate>
</RadioButton.Template>
单个项目的ViewModel

public class CountryVm
{
    public CountryVm()
    {
        ImageUri = new Uri("Resources/rgb.png", UriKind.Relative);            
    }

    public string Name { get; set; }

    public Uri ImageUri { get; set; }
}
列表框标记

<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。它会自动工作的。在我看来,目前的代码有一点收入变化,但无论如何,感谢您从不同的角度看待这个问题。
<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>
DataContext = new List<CountryVm>
{
    new CountryVm {Name = "123"},
    new CountryVm {Name = "Abc"},
    new CountryVm {Name = "Xyz"},
};