2013-04-20 91 views
0

我試着在我的用戶設置:如何將DataContext添加到usercontrol的xaml?

d:DataContext="{d:DesignData ListItemDemoData.xaml}" 

但Visual Studio中說,它無法找到ListItemDemoData,不ListItemDemoData.xaml但只是ListItemDemoData

奇怪,我做錯了嗎?

+0

ListItemDemoData.xaml位於何處?在同一個文件夾中? VS中的設計數據非常麻煩:/ – 2013-04-20 13:15:59

+0

它是完全相同的文件夾是 – Jason94 2013-04-20 16:13:47

+0

您是否檢查過ListItemDemoData.xaml的構建操作?它應該設置爲** DesignData **。另外,你可以嘗試聲明如下:'d:DataContext =「{d:DesignData Source = ./ListItemDemoData.xaml}' – 2013-04-22 11:44:10

回答

1

你可以在後面的代碼上做到這一點。 我有一個用戶控件用來顯示一個大的圖像。我的控件的名稱是ImageViewer。現在我必須綁定圖像源。所以,我創建一個依賴屬性等

public static readonly DependencyProperty ValueProperty = 
     DependencyProperty.Register("ImageSourceValue", typeof(string), typeof(ImageViewer), 
     new PropertyMetadata(ValueChanged)); 

    public string ImageSourceValue 
    { 
     get { return (string)GetValue(ValueProperty); } 
     set { SetValue(ValueProperty, value); } 
    } 

    /// <summary> 
    /// Called when the value of ImageSourceValue is changed or set. 
    /// </summary> 
    public static void ValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) 
    { 
     // Get out of a static and into the instance ASAP. 
     ImageViewer control = (ImageViewer)sender; 
     control.ValueChanged(); 

    } 

    private void ValueChanged() 
    { 
     //The value of ImageSourceValue is set at the time calling user control from your page. 
     this.ImageSource = ImageSourceValue; 

     this.DataContext = this; 
    } 

    public string ImageSource { get; set; } 

我在值被綁定到其將字符串圖像路徑的BitmapImage對象的圖像控制時所使用的轉換器。字符串ImageSource可以更改爲BitmapImage類型。並且可以在將ImageSourceValue的值分配給ImageSource時完成轉換。

+0

錯誤的問題? – 2013-04-22 12:04:04

相關問題