2

I have a UserControl that has an Image and a Textblock, like so:

<Image Stretch="UniformToFill" Source="{Binding Path=ImageURL}"/>
<TextBlock Text="{Binding Path=Title}"/>

in the .cs of the user control i have Dependency properties to make the binding:

    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), typeof(MyUserControl), null);

    public string ImageURL
    {
        get { return (string)GetValue(ImageURLProperty); }
        set { SetValue(ImageURLProperty, value); }
    }

    public static readonly DependencyProperty ImageURLProperty =
        DependencyProperty.Register("ImageURL", typeof(string), typeof(MyUserControl), null);

    public MyUserControl()
    {
        this.InitializeComponent();
        (this.Content as FrameworkElement).DataContext = this;
    }

In my MainPage.xaml i call it inside a list

<ListView Grid.Row="1"
          HorizontalAlignment="Center"
          Name="ControlsListView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <controls:MyUserControl Margin="20"
                                  Title="{Binding Title}" //works
                                  ImageURL="{Binding ImageURL}"/> //doesn't work
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And then i get this Data from Json.net and feed it to the ItemsSource of the ListView. It works just for the Title but no for the ImageURL, anyone could help me why?

Thiago Valle
  • 449
  • 1
  • 6
  • 20

3 Answers3

0

Probably a bad DataContext, see ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext". The answer describes how to use the free Snoop utility to detect runtime binding errors.

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305
0

By default, the DataContext for a UserControl is not set to point at the code behind.

Add this:

<UserControl DataContext="{Binding RelativeSource={RelativeSource Self}}">
Contango
  • 76,540
  • 58
  • 260
  • 305
  • this is actually working, the text works, the image doesn't. If I change the image to a text it will appear the Url of the image – Thiago Valle May 29 '15 at 23:02
  • On second thoughts, you are setting the DataContext in code behind. I normally set it in XAML. – Contango May 30 '15 at 08:22
0

Perhaps the backslashes in your ImageUrl are confusing things.

What ImageUrl are you using? Json might be interpreting the backslash as an escape character, and you might have to encode the ImageUrl using a double backslash.

Contango
  • 76,540
  • 58
  • 260
  • 305