2015-05-29 29 views
2

我有具有圖像一個用戶控件和一個正文塊,像這樣:結合在用戶控件

<Image Stretch="UniformToFill" Source="{Binding Path=ImageURL}"/> 
<TextBlock Text="{Binding Path=Title}"/> 
在用戶控制的的.cs

我有相關性屬性,使結合:

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

在我的MainPage.xaml中我把它叫做一個列表裏面

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

然後,我從Json.net得到這個數據,並將其輸送給它ListView的emsSource。它只適用於標題,但不適用於ImageURL,任何人都可以幫助我爲什麼?

+0

其實這最後還是挺好的。問題是我正在測試一個電話,這個特定的手機有一個問題,沒有渲染圖像從網上喂(耶)。 –

+0

這裏有什麼程序?我應該刪除這個問題嗎? –

回答

0

默認情況下,UserControl的DataContext未設置爲指向後面的代碼。

補充一點:

<UserControl DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
+0

這實際上是工作,文字作品,圖像不。如果我將圖像更改爲文本,則會出現圖像的Url –

+0

請參閱下面的答案。 – Contango

+0

第二個想法是,你在後面的代碼中設置DataContext。我通常將其設置在XAML中。 – Contango

0

也許在你的ImageUrl反斜線是令人困惑的事情。

您使用的是什麼ImageUrl? Json可能會將反斜槓解釋爲轉義字符,並且您可能必須使用雙反斜槓對ImageUrl進行編碼。