替换单选按钮上的图像

时间:2016-08-19 03:31:16

标签: c# image radio-button uwp uwp-xaml

如何更换单选按钮上的图像? 我想为每个单选按钮使用下面的图像。

radio button image

XAML:

 <RadioButton x:Name="FreshBtn" Content="Fresh" Style="{StaticResource TabRadioButtonStyle}" IsChecked="True" Click="FreshBtn_Click" FontSize="23" Grid.Column="0"/>
 <RadioButton x:Name="PopularBtn" Content="Popular" Style="{StaticResource TabRadioButtonStyle}" IsChecked="False" Margin="0,0,0,0" Click="PopularBtn_Click" FontSize="23" Grid.Column="1"/>
 <RadioButton x:Name="FreeBtn" Content="Free" Style="{StaticResource TabRadioButtonStyle}" IsChecked="False" Margin="0,0,0,0" Click="FreeBtn_Click" FontSize="23" Grid.Column="2"/>

我还想询问如何在点击时更改单选按钮上的图像?

1 个答案:

答案 0 :(得分:1)

我假设您要更改单选按钮的背景图片。为此,您可以执行以下操作:

<RadioButton x:Name="FreshBtn" Content="" Style="{StaticResource TabRadioButtonStyle}" IsChecked="True" Click="FreshBtn_Click" FontSize="23" Grid.Column="0" Margin="10,302,0,301" Checked="FreshBtn_Checked">
    <RadioButton.Background>
        <ImageBrush ImageSource="/Assets/myImage.jpg"></ImageBrush>
    </RadioButton.Background>
</RadioButton>

要在单击单选按钮时更改背景,您需要阻止Clicked或Changed事件处理程序。以下是单击的示例,它将在单击单选按钮时更改背景:

private void FreshBtn_Click(object sender, RoutedEventArgs e)
{
    FreshBtn.Background = new ImageBrush
    {
        Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill,
        ImageSource =
            new BitmapImage { UriSource = new Uri("ms-appx:///Assets/myImage_2.png") }
    };
}

为了在单击单选按钮时更改图片,您仍然需要实现“Clicked”事件处理程序,以便在单击单选按钮时更改图像,上面的代码应该可以使用。

相关问题