I have the following code for a Custom WPF Radio Button that has an image as content. The image Built Action is set to "Content" and the Copy to Output Directory to "Copy if newer".
<!--MaterialButtonTheme-->
<Style BasedOn="{StaticResource {x:Type ToggleButton}}"
TargetType="{x:Type RadioButton}"
x:Key="MaterialButtonTheme">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Grid VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Background="{TemplateBinding Background}">
<Border>
<Image Source="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Opacity" Value=".3"/>
<Setter Property="BorderThickness" Value="0"/>
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value=".8"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Opacity" Value=".6"/>
</Trigger>
</Style.Triggers>
</Style>
Now, I don't want to use the hard code variant for the image resource, but the following does not work.
<RadioButton
x:Name="RadioButtonSteelMat"
Height="30" Width="30"
Foreground="White"
HorizontalAlignment="Left"
FontSize="14"
Style="{StaticResource MaterialButtonTheme}"
Content="../EmbeddedResources/Images/steel.png"
IsChecked="False"
Margin="10,200,0,0"
Checked="RadioButtonSteelMat_Checked"
/>
The following hard coded variant works just fine. Does anybody know what I'm doing wrong?
<RadioButton
x:Name="RadioButtonSteelMat"
Height="30" Width="30"
Foreground="White"
HorizontalAlignment="Left"
FontSize="14"
Style="{StaticResource MaterialButtonTheme}"
Content="C:\Users\Niklas\Desktop\Studium\Master\M4\Thesis\Tool\OptioneeringTool\OptioneeringTool\B GOpt\EmbeddedResources\Images\steel.png"
IsChecked="False"
Margin="10,200,0,0"
Checked="RadioButtonSteelMat_Checked"
/>
CodePudding user response:
Content="/EmbeddedResources/Images/steel.png"
should work assuming the EmbeddedResources folder is located at the root your project folder in Visual Studio when you build and that the Built Action
of the image file is set to "Content" and the Copy to Output Directory
property to "Copy if newer".
The other option is to set the Built Action
to "Resource" and use a pack URI to reference it:
pack://application:,,,/EmbeddedResources/Images/steel.png