Wpf Register(“NormalImageUriString”、typeof(string)、typeof(SampleRadioButton)、newpropertyMetadata(null)); 公共字符串checkedImageUrString { 获取{return(string)GetValue(CheckedImageUriStringProperty);} set{SetValue(CheckedImageUriStringProperty,value);} } 公共静态只读依赖项属性检查ImageUristingProperty= Register(“checkedImageUristing”、typeof(string)、typeof(SampleRadioButton)、newpropertyMetadata(null)); } 公共类ImageUristingConverver:IValueConverter { 公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性) { if(值不是字符串源) 返回dependencProperty.unset值; 返回新的位图图像(新Uri(source,UriKind.Relative)); } 公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性) { 抛出新的NotImplementedException(); } }
并编辑XAML以使用转换器绑定这些依赖项属性Wpf Register(“NormalImageUriString”、typeof(string)、typeof(SampleRadioButton)、newpropertyMetadata(null)); 公共字符串checkedImageUrString { 获取{return(string)GetValue(CheckedImageUriStringProperty);} set{SetValue(CheckedImageUriStringProperty,value);} } 公共静态只读依赖项属性检查ImageUristingProperty= Register(“checkedImageUristing”、typeof(string)、typeof(SampleRadioButton)、newpropertyMetadata(null)); } 公共类ImageUristingConverver:IValueConverter { 公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性) { if(值不是字符串源) 返回dependencProperty.unset值; 返回新的位图图像(新Uri(source,UriKind.Relative)); } 公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性) { 抛出新的NotImplementedException(); } },wpf,xaml,radio-button,Wpf,Xaml,Radio Button,并编辑XAML以使用转换器绑定这些依赖项属性 <local:ImageUriStringConverver x:Key="ImageUriStringConverterKey"/> <Style TargetType="{x:Type local:SampleRadioButton}"> <Setter Property="NormalImageUriString" Value="[UR
<local:ImageUriStringConverver x:Key="ImageUriStringConverterKey"/>
<Style TargetType="{x:Type local:SampleRadioButton}">
<Setter Property="NormalImageUriString" Value="[URI string to default image for normal state]"/>
<Setter Property="CheckedImageUriString" Value="[URI string to default image for checked state]"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SampleRadioButton}">
<Grid Background="{TemplateBinding Background}">
...
<Image x:Name="SidebarRadioButtonMenuIcon"
Source="{TemplateBinding NormalImageUriString, Converter={StaticResource ImageUriStringConverterKey}}"
...
/>
<TextBlock x:Name="SidebarRadioButtonMenuText"
...
/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="SidebarRadioButtonMenuText" Property="Foreground" Value="#F54342" />
<Setter TargetName="SidebarRadioButtonMenuIcon" Property="Source" Value="{Binding CheckedImageUriString, RelativeSource={RelativeSource AncestorType={x:Type local:SampleRadioButton}}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
...
然后,我们可以通过在每个SampleRadioButton中设置这些依赖属性来设置特定的图像
<local:SampleRadioButton Content="Sample"
NormalImageUriString="[URI string to specific image for normal state]"
CheckedImageUriString="[URI string to specific image for checked state]"/>
我有一组单选按钮。我已经在app.xaml中注册了资源字典。让我更新问题以使其更清晰。如果下面的答案不能回答问题,我将更新它。图像相同的原因是您为这两个问题提供了相同的图像路径。可能是因为这个吗?RadioButton的画法在我画的时候很有效,并给出了不同的图片。是的,出于测试目的,它是一样的。但是,如果我更改每个项目的默认和“选定”图像,是否需要为每个按钮创建另一个资源字典?我有一组单选按钮。我已经在app.xaml中注册了资源字典。让我更新问题以使其更清晰。如果下面的答案不能回答问题,我将更新它。图像相同的原因是您为这两个问题提供了相同的图像路径。可能是因为这个吗?RadioButton的画法在我画的时候很有效,并给出了不同的图片。是的,出于测试目的,它是一样的。但是,如果我更改每个项目的默认和“选定”图像,是否需要为每个按钮创建另一个资源字典?我已经这样做了。请看我更新的帖子:DI无法根据xaml端的IsChecked true和false更改背景图像。这可以通过花一点时间来解决。这就是我所能做的:)好吧,这解决了“默认”图像显示(当按钮未点击时)。我仍然需要了解如何根据按钮状态更改图像。您可以通过使用RadioBt.Tag=“/Images/image.png”为RadioButton指定名称来更改图像代码>在后台代码中。如何在代码隐藏中执行此操作?这是我第一次在wpf中创建ui。我已经这样做了。请看我更新的帖子:DI无法根据xaml端的IsChecked true和false更改背景图像。这可以通过花一点时间来解决。这就是我所能做的:)好吧,这解决了“默认”图像显示(当按钮未点击时)。我仍然需要了解如何根据按钮状态更改图像。您可以通过使用RadioBt.Tag=“/Images/image.png”为RadioButton指定名称来更改图像代码>在后台代码中。如何在代码隐藏中执行此操作?这是我第一次在wpf中创建ui。@Hacki,我认为这似乎是理想的解决方案。当您想向RadioButton添加新功能时,您可以在单个结构中进行必要的安排。我们真的需要在xaml中使用这些功能吗?我尝试了这个,但是,我的正常/默认图像不显示。但是,当我选择图像时,它现在会根据我选择的菜单项更改图像。我似乎无法显示正常/默认图像,因此我将您的答案与@Saklamaz的答案混合在一起,现在它可以工作了!(对我的预期行为)此答案:选择RadioButton时更改了它的图像Saklamaz的答案:显示RadioButton的默认图像(未选择时)现在我不知道该接受谁;(如果您选择在每个SampleRadioButtons中设置特定的图像,则不需要设置默认图像。图像的URI字符串的正确格式取决于您在应用程序中如何包含这些图像。这只是我认为简单方法的一个示例。@Hacki,我认为这似乎是理想的解决方案。如果您想添加RadioButton的新功能,您可以在单个结构中进行必要的排列。我们真的需要在xaml中使用这些功能吗?我尝试过,但是,我的正常/默认图像没有显示。但是,当我选择一个图像时,它现在会根据我选择的菜单项更改其图像。我似乎无法显示正常/默认图像,因此我把你的答案和@Saklamaz的答案混在一起了,现在它起作用了!(我的预期行为)这个答案:当我选择它时,改变了单选按钮的图像Saklamaz的答案:显示单选按钮的默认图像(未选择时),现在我不知道该接受谁;(如果您选择在每个SampleRadioButtons中设置特定的图像,则不需要设置默认图像。图像的URI字符串的正确格式取决于您如何在应用程序中包含这些图像。这只是我认为简单方法的一个示例。
<local:ImageUriStringConverver x:Key="ImageUriStringConverterKey"/>
<Style TargetType="{x:Type local:SampleRadioButton}">
<Setter Property="NormalImageUriString" Value="[URI string to default image for normal state]"/>
<Setter Property="CheckedImageUriString" Value="[URI string to default image for checked state]"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SampleRadioButton}">
<Grid Background="{TemplateBinding Background}">
...
<Image x:Name="SidebarRadioButtonMenuIcon"
Source="{TemplateBinding NormalImageUriString, Converter={StaticResource ImageUriStringConverterKey}}"
...
/>
<TextBlock x:Name="SidebarRadioButtonMenuText"
...
/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="SidebarRadioButtonMenuText" Property="Foreground" Value="#F54342" />
<Setter TargetName="SidebarRadioButtonMenuIcon" Property="Source" Value="{Binding CheckedImageUriString, RelativeSource={RelativeSource AncestorType={x:Type local:SampleRadioButton}}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<local:SampleRadioButton Content="Sample"
NormalImageUriString="[URI string to specific image for normal state]"
CheckedImageUriString="[URI string to specific image for checked state]"/>