[英]Binding image source to property not working
我在此視圖模型中的其他綁定工作正常。 我不確定為什么沒有這個。 我嘗試了多種不同的方法來更新具有綁定的圖像源。 都失敗了。 這是我見過的最常見的方法,也是我在Windows Phone上完成的方法。 我不確定為什么這在WPF中不起作用。
XAML
<ComboBox
ItemsSource="{Binding Keywords}"
SelectedItem="{Binding SelectedKeyword, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Width="280" Content="{Binding KeywordName}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Grid>
<Image Source="{Binding LinkedImage}"/>
</Grid>
視圖模型
public KeywordObject SelectedKeyword
{
get { return _SelectedKeyword; }
set { _SelectedKeyword = value; OnPropertyChanged("LinkedImage"); }
}
public Boolean _IsLinked
{
get { return _SelectedKeyword.KeywordNumber.Length > 0; }
}
public ImageSource LinkedImage
{
get
{
return _IsLinked ?
new BitmapImage(new Uri("/images/checked.png", UriKind.RelativeOrAbsolute))
:
new BitmapImage(new Uri("/images/unchecked.png", UriKind.RelativeOrAbsolute));
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string prop)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
這是調試器中輸出的異常
A first chance exception of type 'System.IO.IOException' occurred in PresentationFramework.dll
A first chance exception of type 'System.IO.IOException' occurred in PresentationCore.dll
答案最終是路徑需要pack://application:,,,/Specific.Project.Name;component
這是回答它的SO文章的鏈接
該圖像顯示在Visual Studio設計器中,但不在運行時顯示
這是最終結果的樣子
<image Source="pack://application:,,,/Specific.Project.Name;component/images/checked.png"/>
我也能夠將其注入到綁定中,並且效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.