2016-05-09 9 views
0

イメージをユーザーコントロールに埋め込もうとしています。私はこの話題について多くの記事を見ていますが、私は多くの組み合わせを試みましたが、うまく動作することはできません。コードでUserControlのImageSourceの設定時にTypeConverterMarkupExtensionエラーが発生しました。

<UserControl x:Class="AudioBoxController.AudioBoxItem" 
      x:Name="AudioBoxItemControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" Width="200" Height="250"> 
    <Grid> 

    <Image Name="image" Margin="10" VerticalAlignment="Center" 
      Source="{Binding Path=ImageSource, Mode=OneWay}"/> 

    </Grid> 
</UserControl> 

私はイメージのためのDPを作成する必要があり後ろに:私は正しく画像を参照してください設計時に

<local:AudioBoxItem x:Name="ctrlMike" 
        Grid.Column="0" 
        VerticalAlignment="Center" 
        ImageSource="/AudioBoxController;component/Images/Speakers.png"/> 

public partial class AudioBoxItem : UserControl 
{ 
    public AudioBoxItem() 
    { 
    InitializeComponent(); 
    DataContext = this; 
    } 

    public static DependencyProperty SourceProperty = 
     DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(AudioBoxItem)); 

    public ImageSource ImageSource 
    { 
    get { return (ImageSource)GetValue(SourceProperty); } 
    set { SetValue(SourceProperty, value); } 
    } 
} 

さて、ウィンドウに私はそれを使用します代わりに、私が実行するとエラーが発生します:

私は間違っている?

EDIT

は、私が「コンテンツ」に「埋め込まれたリソース」から元の画像を変更し、私は相対パスを使用します。それは動作します。このように

<local:AudioBoxItem x:Name="ctrlMike" 
        Grid.Column="0" 
        VerticalAlignment="Center" 
        ImageSource="Images/Speakers.png"/> 

を...そう、これ構文イメージが「組み込みリソース」の場合は使用する必要がありますか?

+0

をサポートするために、あなたのDependencyProperty宣言にPropertyMetadata引数を追加する必要があると考えていImageSourceProperty

でなければなりませんが、あなたは 'パックを試してみました://アプリケーションを:,,,/Images/Speakers.png'? – Sinatr

+1

イメージファイルを[Resource File Pack URI](https://msdn.microsoft.com/en-us/library/aa970069(v = vs.100).aspx#Resource_File_Pack_URIs ___ Local_Assembly)でロードする場合は、ビルドアクションは 'Content'の' Embedded Resource'ではなく 'Resource'でなければなりません。 – Clemens

+1

それ以外では、一般に、UserControlのDataContextを明示的に設定することは避けてください(例えば、コンストラクタで 'DataContext = this'など)。 'Source =" {Binding ImageSource、RelativeSource = {RelativeSource AncestorType = UserControl}} "のようにRelativeSourceを使ってバインディングを書くほうがいいです。 ModeをOneWayに設定するのは、デフォルトで一方向であるため、冗長です。 – Clemens

答えて

0

問題は、DependencyProperty宣言にあります。

まず、SourcePropertyは第二に、私はあなたがOneWayバインディング

public static DependencyProperty SourceProperty = 
     DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(AudioBoxItem), new PropertyMetadata(null)); 
関連する問題