WPF でイメージ ボタンを作成しようとしています。私がやったことは
- Button から継承されたユーザー コントロールを作成し、必要なイメージを持つ依存関係プロパティを宣言します。
- リソース ディクショナリで、その xaml テンプレートを宣言します。
- その依存関係プロパティに相対パスとフルパスを渡します。フルパスは機能しますが、相対パスは機能しません。
ユーザー コントロール クラス
public class ButtonWithImage : Button
{
public ImageSource ButtonImage
{
get { return (ImageSource)GetValue(ButtonImageProperty); }
set { SetValue(ButtonImageProperty, value); }
}
// Using a DependencyProperty as the backing store for ButtonImage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonImageProperty =
DependencyProperty.Register("ButtonImage", typeof(ImageSource), typeof(ButtonWithImage));
}
リソース ディクショナリ コード
<Style x:Key="ButtonWithImageStyle" TargetType="complaintRegister:ButtonWithImage">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="complaintRegister:ButtonWithImage">
<StackPanel>
<Image Source="{Binding ButtonImage, RelativeSource={RelativeSource TemplatedParent}}" Width="50" Height="50"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
XAML
<complaintRegister:ButtonWithImage x:Name="ButtonAdd" Content="Add New Complaint" Style="{StaticResource ButtonWithImageStyle}"
ButtonImage="Resources\Plus.png"
Width="150" Height="75" Margin="10 0" Command="{Binding NewCommand}">
</complaintRegister:ButtonWithImage>
エラー
デザインモードで画像を表示しますが、実行時にこの例外をスローします
Cannot locate resource 'resources/plus.png'
「Resources/Plus.png」ではなく「resources/plus.png」を解決しようとしていると思わざるを得ません。