使用Windows 8 Developer Preview。如果類型爲WinRT中的ImageSource,則會綁定到UserControl屬性的錯誤
我有這樣的屬性之一的對象:
private ImageSource _Image = null;
public ImageSource Image
{
get
{
return this._Image;
}
set
{
if (this._Image != value)
{
this._Image = value;
this.OnPropertyChanged("Image");
}
}
}
public void SetImage(Uri baseUri, String path)
{
Image = new BitmapImage(new Uri(baseUri, path));
}
這在一個ObservableCollection像這樣使用:
var test = new ObservableCollection<object>();
ButtonItem item = new ButtonItem();
item.SetImage(this.basUri, "Data/Images/test.png");
凡test.png被包括作爲內容。
此集合用於設置一個網格的的ItemsSource,像這樣:
ItemGridView.ItemsSource = test;
而這個網格具有的DataTemplate:
<DataTemplate x:Key="testtemp">
<Grid HorizontalAlignment="Left" Background="White">
<StackPanel Orientation="Horizontal" Margin="10,10,0,0">
<my:MyButton Image="{Binding Image}"></my:MyButton>
</StackPanel>
</Grid>
</DataTemplate>
這myButton爲其中所述圖像的用戶控制屬性是這樣的依賴屬性:
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("Image", "ImageSource", typeof(VSButton).FullName, null);
public ImageSource Image
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set
{
SetValue(ImageSourceProperty, value);
}
}
現在,當我運行這個我得到一個前ception:
「」類型的異常出現在將Test.exe但在用戶代碼中沒有處理
其他信息:無法轉換類型的COM對象「Windows.UI.Xaml。 Data.Binding」類類型‘Windows.UI.Xaml.Media.ImageSource’
現在...當我轉換屬性的用戶控件字符串類型(和綁定爲一個字符串),一切正常如預期的那樣,所以我必須做一些其他的事情..什麼?
我知道了,我做了..爲了簡單起見,我將此問題更改爲ObservableCollection。不工作 – Flores 2012-02-14 18:44:53
哦,那麼這不是問題。通過你的代碼看,它看起來很好。嘗試在註冊時初始化您的PropertyMetadata。我要試着運行這段代碼。 – sarvesh 2012-02-14 18:49:57
設置PropertyMetaData也沒有幫助。 – Flores 2012-02-14 22:02:15