简体   繁体   中英

TypeConverterMarkupExtension error on setting ImageSource of UserControl

I'm trying to embed an Image into a User Control. I see many posts about this topic, and I tried a lot of combination, but I cannot get it to work.

<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>

In code behind I have create a DP for the Image:

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); }
  }
}

Now, in the window I use it:

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

At design time I correctly see the image, instead, when I run I get the error:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: 'An exception was thrown when the specification of a value of
'System.Windows.Baml2006.TypeConverterMarkupExtension'.' line number '19' e line position '60'.

Where I'm wrong?

EDIT

I changed the source image from "Embedded Resource" to "Content" and I use the relative path:

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

In this way it works...so, which syntax I have to use if the image is an "embedded Resource"?

Your problem is with your DependencyProperty declaration.

Firstly, SourceProperty should be ImageSourceProperty

Secondly, I believe you need to add a PropertyMetadata argument to your DependencyProperty declaration in order to support OneWay bindings

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM